필터 지우기
필터 지우기

How to write multiple outputs to a .txt file?

조회 수: 4 (최근 30일)
Alessandro Togni
Alessandro Togni 2021년 2월 9일
댓글: Alessandro Togni 2021년 2월 10일
Hi,
my task is to create a .txt file containing an header (strings +numbers) and a then a matrix, semicolumns separated.
That's my code:
string_outp= [string(app.file_geo), "from", string(from_value), "to", string(to_value), ".geo.txt"];
output_file_name= join(string_outp, "_")
fid = fopen(string(output_file_name),'wt');
fprintf(fid, '%s\n', app.geo_header); % Writes the header correctly
R_mod=vertcat(app.R(from_value:to_value, :));
writematrix(R_mod, string(output_file_name))% Writes the matrix correctly
fclose(fid);
problem is: the writematrix commanda overwrites the header.
How to write the header and then the matrix?
Thanks in advance,
Alessandro

채택된 답변

Jan
Jan 2021년 2월 9일
편집: Jan 2021년 2월 9일
string_outp= [string(app.file_geo), "from", ...
string(from_value), "to", string(to_value), ".geo.txt"];
output_file_name= join(string_outp, "_")
fid = fopen(output_file_name, 'wt');
fprintf(fid, '%s\n', app.geo_header); % Writes the header correctly
fclose(fid);
R_mod = vertcat(app.R(from_value:to_value, :));
writematrix(R_mod, output_file_name, 'WriteMode', 'append')
So close the file and append the matrix.
Are you sure, that vertcat is needed to contruct R_mod?
  댓글 수: 1
Alessandro Togni
Alessandro Togni 2021년 2월 10일
Thanks for the answer and yes, "vertcat" was not necessary.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by