Borrar filtros
Borrar filtros

How to write multiple outputs to a .txt file?

2 visualizaciones (últimos 30 días)
Alessandro Togni
Alessandro Togni el 9 de Feb. de 2021
Comentada: Alessandro Togni el 10 de Feb. de 2021
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

Respuesta aceptada

Jan
Jan el 9 de Feb. de 2021
Editada: Jan el 9 de Feb. de 2021
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 comentario
Alessandro Togni
Alessandro Togni el 10 de Feb. de 2021
Thanks for the answer and yes, "vertcat" was not necessary.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by