Save each column of a matrix in a seperate .csv file

3 vues (au cours des 30 derniers jours)
monmatlab
monmatlab le 22 Déc 2016
Commenté : monmatlab le 3 Jan 2017
I was wondering how to write this code in a more elegant way. Basically, I want to save each column of the sample matrix in a separate .csv file. I wanted do this in a for loop, but it is not clear for me how to give each filename an index, because Channel(i).csv ,is just considered to be the filename without index
channel1=sample(:,1);save Channel1.csv channel1 -ASCII;
channel2=sample(:,2);save Channel1.csv channel2 -ASCII;
channel3=sample(:,3);save Channel1.csv channel3 -ASCII;
channel4=sample(:,4);save Channel1.csv channel4 -ASCII;
channel5=sample(:,5);save Channel1.csv channel5 -ASCII;
channel6=sample(:,6);save Channel1.csv channel6 -ASCII;
...
...
....
channel15=sample(:,15);save Channel1.csv channel15 -ASCII;
channel16=sample(:,16);save Channel1.csv channel16 -ASCII;

Réponse acceptée

per isakson
per isakson le 22 Déc 2016
Modifié(e) : per isakson le 22 Déc 2016
  • "save each column of the sample matrix in a separate .csv file" &nbsp e.g. Channel01, Channel02, etc.
  • "how to give each filename an index" &nbsp make the number part of the name, e.g. sprintf('Channel%02d.csv',jj). The leading zero, "0", makes the files appear in the "correct" order in file listings.
  • I prefer fprintf over save to write text
sample = [ magic(6); magic(6) ]; % Create sample data
len = size( sample, 2 );
folder = 'h:\m\cssm';
for jj = 1 : len
ffs = fullfile( folder, sprintf('Channel%02d.csv',jj) );
fid = fopen( ffs, 'w' );
fprintf( fid, '%f\n', sample(:,jj) );
fclose( fid );
end

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by