MATLAB: How to convert a .mat file into a .csv file

digital image processingimageimage processing

I have a .mat file loaded in workspace. I want to convert it into .csv file.
FileData = load('Trainset.mat');
csvwrite('FileName.csv', FileData.M);
I used the above method to convert but i get this error. >> csv_Con Reference to non-existent field 'M'.
Error in csv_Con (line 2) csvwrite('FileName.csv', FileData.M);
>> >> >>

Best Answer

FileData = load('Trainset.mat');
fields = fieldnames(FileData);
for K = 1 : length(fields)
thisvar = fields{K};
thisdata = FileData.(thisvar);
if ~isnumeric(thisdata)
warning('Skipping field %s which is type %s instead of numeric', thisvar, class(thisvar));
else
thisfile = sprintf('FileName_%s.csv', thisvar);
csvwrite(thisfile, thisdata);
end
end