convert mat-files into CSV.files

조회 수: 7 (최근 30일)
Zahra  S. Abd Al-Hassan
Zahra S. Abd Al-Hassan 2017년 5월 5일
댓글: dpb 2020년 7월 19일
Hey
I want to convert my mat-files to CSV. I have 20 mat-files. They are all in the same folder, so I want to convert everything in this folder to af CSV-file and they end with .mat How can I make a loop that takes the 20 subjects and convert the mat file into CSV? I have tried this but its not working, and of course I have to change the filename every time and its time-consuming:

채택된 답변

dpb
dpb 2017년 5월 5일
d=dir(fullpath(dirname,'*.mat'));
for i=1:length(d)
load(d(i).name % will leave whatever variable is in mat-file in memory
[p,n]=fileparts(d(i).name); % get path, name w/o extension
csvwrite([fullfile(p,n) '.csv'],X) % write to name w/ .csv extension
end
The above use X as a placeholder for the variable containing the data LOADed; use whatever is that name. Of course, also assumes all the files are symmetric with the same name; if not use the functional form of load and the appropriate structure name retrieved.
And, of course, this begs the question of why one would do this... .mat files are much more accurate in keeping full precision whereas .csv files are a limited number of decimal places; are much faster to read/write and take up far less disk space besides (albeit that's pretty much a "don't care" item any more).
  댓글 수: 4
Sachin Patalasingh
Sachin Patalasingh 2020년 7월 17일
Can you please suggest me how to perform the above task when the files are named in a haphazard way ?
dpb
dpb 2020년 7월 19일
Well, you can only work with what are given..
d=dir(fullpath(dirname,'*.mat'));
will return all .mat files; the filenames can be any OS-legal name. Can't get much more haphazard than that it would seem.
What's the objective?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by