How to convert a .mat file into a .csv file?

Hello Friends,
I have a .mat file loaded in workspace. I want to convert it into .csv file. It has real entries. I used to convert it before using the following commands without any problem:
M = dlmread('FileName.mat', '\t', 1, 0);
csvwrite('FileName.csv', M)
But now something is wrong with MATLAB. My dataset is the same. Nothing has changed, but now it gives the following error:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==>
}Ûa&Ír!ÃþxbSoÈñæo8k:¬i’³*Y#~!IjË9B¯ÝWG~
¬zN=å0ÉÀo8Ó'Rå@õR[iãY>,xÊ8UrVÀ9ó";~~ÀOWAe$Æ\:xfnFVAÿñϼÓЪÍðaþ\n
I wonder if MATLAB changed something with dlmwrite function! Please advise.

4 Comments

Please attach your filename.mat and filename.csv.
I want to convert data from SPM tool to .csv format. SPM stores a file in .mat and .dat format. structure is saved in .mat and .dat file contain actual data. Please tell me way to use both of these files and convert to CSV. Also, how I can retrieve the 4 trials in my data.
how I can said the link where will be created the file
@Jose Ocampo I don't know what you mean. When you do
csvwrite('FileName.csv', M)
the file is created in the current directory -- what you see in the directory/address field in MATLAB.

Sign in to comment.

 Accepted Answer

FileData = load('FileName.mat');
csvwrite('FileName.csv', FileData.M);

17 Comments

Thank you for your answer. I really appreciate this. It definitely works, however, my problem was with the following line of code. This is what I meant that used to work before:
M = dlmread('FileName.mat', '\t', 1, 0);
Here is a working example:
>> A = randn(3,2);
>> M = dlmwrite('A.mat', '\t', 1, 0);
Error using dlmwrite
Too many output arguments.
Did you perhaps create the .mat file with the -ascii option before, and now you're not?
Jan,
I could load the .mat file successfully, but following error is what I got when I did run csvwrite: >> csvwrite('wData.csv', FileData);
Undefined function 'real' for input arguments of type 'struct'.
Error in dlmwrite (line 195) str = sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));
Error in csvwrite (line 43) dlmwrite(filename, m, ',', r, c);
Do you have any suggestion? Thanks,
What does this show in the command window:
whos FileData
it is coming from command FileData = load('FileName.mat');
That did not answer my question. Please put this line in your code just before line 195:
whos FileData
and tell us what shows up in the command window. We need to see if FileData is a regular 2-D array of numbers, or if it's a structure, in which case you'll need to figure out exactly what fields you want to write out with dlmwrite().
Yes, it is structure.
You cannot write a struct directly to an CSV file. This is not meaningful, because it is not clear how the fields should be handles. csvwrite requires a numerical matrix as input. Please read:
doc csvwrite
hye,but this piece of code not working.i am getting error in this code.
I guess since you've posted an unanswerable comment, you have not read this link.
Once you do, you'll know what to do to allow us to answer you.
This function gives an error.
@Abhaya did you read my last comment. Your question is also unanswerable. I don't even know what function you ran.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
@Yassine, if you want help, you will have to provide more details. We can't read your mind or your screen.
mydata = load('FileName.mat');
csvwrite('mydata.csv', mydata);
This works ;)
mydata = load('wind.mat');
csvwrite('mydata.csv', mydata);
Incorrect number or types of inputs or outputs for function real.

Error in csvwrite (line 48)
throw(e)
% not in general it doesn't ;)
@Parisa, your mydata is a structure so you'd have to extract the matrix from the structure first since csvwrite does not take structures as inputs. Something like
% Read variables into fields of a structure.
storedStructure = load('FileName.mat');
% Extract matrix called Data from the structure:
myMatrix = storedStructure.Data; % Change names to whatever you actually have.
% Write the extracted matrix out to a CSV file.
fullFileName = fullfile(pwd, 'MyData.csv'); % Get full path and name.
csvwrite(fullFileName, myMatrix); % Write matrix out to disk.

Sign in to comment.

More Answers (3)

simply you can copy and paste the matrix you want in excel
  • all you have to do is open .mat file in matlab , press 'ctr+A' & 'ctr+C' in the variable tab and paste it in new excel sheet

2 Comments

but what if the data set is very large it will not work then
@Ayman once you have the variable in MATLAB, then you can use writematrix to write it to a disk file, then use File/Open in Excel to read it into Excel.

Sign in to comment.

Ashlesh Sortee
Ashlesh Sortee on 4 Jul 2017
Edited: Ashlesh Sortee on 4 Jul 2017
FileData = load('FileName.mat'); csvwrite('FileName.csv', FileData.FileName);
  • | | * This one worked for me||*
  • Thanks Jan Simon
You're trying to save the variable called FileName20.mat in dlmwrite(). If that's the variable, then you have a dot in the name which means that FileName20 must be a structure, and mat must be a field/member of the structure. It's clearly not - you don't have any structure variable in your program called FileName20. You probably need to pass in M instead of FileName20.

2 Comments

I do not believe that this has worked before. You cannot read a MAT file by dlmread. MAT files are imported by load.
I totally agree with Jan. See Jan's answer, then "Accept" it. It loads your file into a structure called FileData. The "M" array that you stored in the mat file will be a "field" of FileData. Then he writes out only that field to your new csv file.

Sign in to comment.

Asked:

on 30 Mar 2015

Edited:

on 25 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!