convert mat-files into CSV.files
7 views (last 30 days)
Show older comments
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:
0 Comments
Accepted Answer
dpb
on 5 May 2017
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 Comments
Sachin Patalasingh
on 17 Jul 2020
Can you please suggest me how to perform the above task when the files are named in a haphazard way ?
dpb
on 19 Jul 2020
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?
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!