フィルターのクリア

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

1 回表示 (過去 30 日間)
monmatlab
monmatlab 2016 年 12 月 22 日
コメント済み: monmatlab 2017 年 1 月 3 日
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 );
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by