convert mat-files into CSV.files

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:

 Risposta accettata

dpb
dpb il 5 Mag 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 Commenti

Zahra  S. Abd Al-Hassan
Zahra S. Abd Al-Hassan il 5 Mag 2017
Modificato: Zahra S. Abd Al-Hassan il 5 Mag 2017
1) I dont really understand what x is and how to do if my files dont have the same name? The files have the name: modality_seg_D_1_sub001_M1_Mi_k_trafo.mat. They only varies in the segment '1_sub001_M1_Mi_k_' so its only there that the numbers and text changes.
2) Can you give an example with ur files and dir using the code?
3) What do you mean by dirname and fullpath? What is the difference in ur description?
  1. X stands for the variable that is in the .mat file, not the file. I don't know what that is; only you know what was written to each file. I made the presumption that each file will have the same variable stored in it given the apparent symmetry otherwise.
  2. Just run it at the command line without LOAD and CSVWRITE and see...the following will display the first 5 files found and the new name for it...
d=dir(fullpath(dirname,'*.mat'));
for i=1:5 % length(d)
d(i).name
[fullfile(p,n) '.csv']
end
#3 You defined dirname, I just used your variable. fullpath is a Matlab function that concatenates a set of directories ensuring correct punctuation. See
doc fullpath % or any function you don't know for details
4) See
doc load
for description of how to use the structure returned; it's more confusing than helpful to try to write totally generically when don't know your actual situation.
Try
whos -file modality_seg_D_1_sub001_M1_Mi_k_trafo.mat
on command line and it'll tell you the variable name(s) in the file.
Can you please suggest me how to perform the above task when the files are named in a haphazard way ?
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?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by