Read a .mat file and write .csv without opening matlab

90 views (last 30 days)
I would like to read a ".mat" file and write it out as ".csv" file without opening matlab.
Basically this:
M = dlmread('FileName.mat', '\t', 1, 0);
csvwrite('FileName.csv', M)
but then without opening matlab.
I'm using linux command line.

Accepted Answer

Marc Jakobi
Marc Jakobi on 14 Oct 2016
You won't be able to run a Matlab-file without opening Matlab - unless you use another program that is compatible.
However, you could write a bash script that runs Matlab in a "hidden" mode:
matlab -nodisplay -nodesktop -r "run /path_to_script/my_script.m"
P. S. I wouldn't recommend csvwrite. fprintf is a lot more flexible and faster.
fid = fopen('filename.csv','w');
fprintf(fid, formatSpec, data);
Walter Roberson
Walter Roberson on 14 Oct 2016
You would want
fprintf(fid, formatSpec, [t, P].');
to write t and P into columns.

Sign in to comment.

More Answers (2)

Michael02139 on 25 Oct 2016
Thanks for all the tips. I'm just lost even after those tips. Which part of the tips are aimed at helping me to read in a *mat file? And which parts are aimed at writing out a *csv file?
Michael02139 on 25 Oct 2016
Thanks a lot for following up and responding again! That helped! I have it working.

Sign in to comment.

Walter Roberson
Walter Roberson on 25 Oct 2016
Is the mat file text with tab delimiters or is it a binary file created by save()?
If it is text with tab delimiters then consider just using sed to change the tabs into comma
  1 Comment
Michael02139 on 25 Oct 2016
Got it! I like "sed" but it wasn't necessary this time.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!