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

조회 수: 4(최근 30일)
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;

채택된 답변

per isakson
per isakson 2016년 12월 22일
편집: per isakson 2016년 12월 22일
  • "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 );

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by