Multiple lines to write using fid open

4 ビュー (過去 30 日間)
lilly lord
lilly lord 2021 年 9 月 14 日
回答済み: Cris LaPierre 2021 年 9 月 14 日
I have a matrix and some operation is performed on the rows e.g average of each row. I want to store those rows whose average is above some condition and if multiple rows satisfy the condition then both should be written in a text file. My code only writes last row [10 11 2] where as I want both 2 and 3rd rows.
M=[2 3 4; 6 7 9; 10 11 2];
R1=sum(M(1,:))/3; % ans is 3
R2=sum(M(2,:))/3;% ans is 7.3333
R3=sum(M(3,:))/3; % ans is 7.6667
T=[R1,R2,R3];
for i = 1:3
if T(i) >= 7.3333
fid = fopen(['Result/T(i)',',','.txt'], 'w');
fprintf(fid,'%d',M(i,:));
fprintf(fid,',');
fclose(fid);
end
end

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 9 月 14 日
The issue is with the permission you are using with fopen ('w').
  • 'w' Open or create new file for writing. Discard existing contents, if any.
You probably want 'a+'
  • 'a+' Open or create new file for reading and writing. Append data to the end of the file.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by