How can I use fprintf to save a txt file with different size matrix ?

2 ビュー (過去 30 日間)
Aris
Aris 2014 年 8 月 6 日
コメント済み: Aris 2014 年 8 月 7 日
Hi,
I am using the following and works fine :
saveAs = [fPath '\' fPrefix fmid '_' trialNo{4} '_FVL.txt'];
fid = fopen(saveAs, 'w');
fprintf(fid, '%s\t %s\t %s\n', 'Time [s]', 'FL [mm]','EMG [v]');
for v = 1: ufsize ;
fprintf(fid, '%d\t %d\t %d\n', Ultratimefinal0(v), lfascicle(v), EMG(v) );
end
fclose(fid);
The problem is when I am trying to cut some values from EMG ex. EMGc = EMG(16:end); then I am getting the "Index exceeds matrix dimensions" error. How can I save these different sized values ? I would like to have something like this:
1 1
1 1
1 1
1 1
1
1
1
1
And something else... how can I move the whole matrix in a specific row ?
1
1
1 1
1 1
1 1
1 1
1
1
Thanks a lot.

採用された回答

Christopher Berry
Christopher Berry 2014 年 8 月 6 日
One way you might do this is to use if-else statements around your fprintf, like this:
fprintf(fid, '%d\t', Ultratimefinal0(v));
if ( v < numel(lfascicle) )
fprintf(fid, '%d\t', lfascicle(v));
else
fprintf(fid, '\t');
end
if ( v < numel(EMG) )
fprintf(fid, '%d\n', EMG(v));
else
fprintf(fid, '\n');
end
This will accomplish the first case you showed... As for the second case, you could shift your whole column by using an offset in the EMG part like this:
offset = 2; % Just like your example, but this could be anything
if ( v>offset && ( v-offset) < numel(EMG) )
fprintf(fid, '%d\n', EMG(v-offset));
else
fprintf(fid, '\n');
end
  1 件のコメント
Aris
Aris 2014 年 8 月 7 日
Thanks Christopher, your answer was really helpful.

サインインしてコメントする。

その他の回答 (1 件)

Robert Cumming
Robert Cumming 2014 年 8 月 6 日
you can loop over the column dimension of your variables when you write the file out, or before the write command check that each of your variables exist - if not customise the fprintf command.
You might also be interested in the function fullfile.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by