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

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.
Thanks!

 採用された回答

Marc Jakobi
Marc Jakobi 2016 年 10 月 14 日

0 投票

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:
#!/bin/bash
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);
fclose(fid);

3 件のコメント

Michael02139
Michael02139 2016 年 10 月 14 日
Hi Marc, thanks for your suggestions.
I'm starting with your suggestion to use fprintf and did this:
>> clear all
>> fid = fopen('FileName.mat','w');
fprintf(fid, formatSpec, data);
fclose(fid);
But I get this:
??? Undefined function or variable 'formatSpec'.
Any clue why? (your example text suggests opening a .csv file but I'm starting with .mat...)
thanks,
Marc Jakobi
Marc Jakobi 2016 年 10 月 14 日
You have to declare formatSpec as a variable first: According to the data you are writing to the CSV file.
Here's an example:
t = (0:900:31535100)';
P = rand(size(t));
fid = fopen('filename.csv','w');
formatSpec = '%d;%d\n';
fprintf(fid, formatSpec, [t, P]);
fclose(fid);
The formatSpec tells to write two columns of doubles (%d), delimeted with a semicolon. "\n" tells it to start a new line.
If you type
doc fprintf
in Matlab's command window, you will find a description and examples of how to use format operators.
Walter Roberson
Walter Roberson 2016 年 10 月 14 日
You would want
fprintf(fid, formatSpec, [t, P].');
to write t and P into columns.

サインインしてコメントする。

その他の回答 (2 件)

Michael02139
Michael02139 2016 年 10 月 25 日

0 投票

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?

2 件のコメント

Marc Jakobi
Marc Jakobi 2016 年 10 月 25 日
That was all about writing a CSV file. Loading a MAT file should be trivial:
load('matFileName.mat')
If you don't know which variables are contained in the mat file, you can load it into a struct:
S = load('matFileName.mat');
varNames = fieldnames(S);
etc.
Michael02139
Michael02139 2016 年 10 月 25 日
Thanks a lot for following up and responding again! That helped! I have it working.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2016 年 10 月 25 日

0 投票

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 件のコメント

Michael02139
Michael02139 2016 年 10 月 25 日
Got it! I like "sed" but it wasn't necessary this time.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by