How to save output in different column for each loop
3 ビュー (過去 30 日間)
古いコメントを表示
I was trying to save the output from each for loop into the different column using fprintf(fid,'%7.4f %7.4f %7.4f %7.4f\n',A). I want the first run to complete and save output, and have the second run and save it into different column. However, it is not saving output into four different column rather saving out in a single one. Any suggestion?
0 件のコメント
回答 (2 件)
jean claude
2017 年 10 月 10 日
編集済み: jean claude
2017 年 10 月 10 日
you have to design the dimension of your output matrix, example if it is 10x3 set i(number of rows) and j(number of columns), than for loop will begin like loop#1 i=1 j=1 loop#2 i=1 j=2 loop#3 i=1 j=3 then loop#4 i=2 j=1 loop#5 i=2 j=2 loop#6 i=2 j=3 ...etc
for i=1:10
for j=1:3
x=randn(1);
a(i,j)=x ;
end
end
you can always use debugging mode https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html to understand how matlab treat your loop
0 件のコメント
Fangjun Jiang
2017 年 10 月 10 日
You could save the result in a temporary variable and do fprintf() at once. For example
A=zeros(4,1);
for k=1:4
A(k,1)=k+rand(1);
end
fprintf(1,'%7.4f %7.4f %7.4f %7.4f\n',A);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!