convert mat-files into CSV.files
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
2020-7-17
Can you please suggest me how to perform the above task when the files are named in a haphazard way ?
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 Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!