How do I save data to a TXT file?
Mostrar comentarios más antiguos
How do I save data to a TXT file? I want to create a simple two column text file, where the first column is the data from a n x 1 matrix and the second column is a different n x 1 matrix. I also want column headings in the first row of each column.
2 comentarios
Raghunandan V
el 4 de Oct. de 2018
Editada: per isakson
el 20 de En. de 2019
the problem is you are trying to store them in an array whereas you have to store them in a cell array since each file is of different size Try this code
fileName={'new1.txt', 'new2.txt', 'new3.txt'};
%open file identifier
fid=fopen('MyFile.txt','w');
for k=1:length(fileName)
%read the file name as string including delimiters and next lines
List=textread(fileName{1,k},'%s','delimiter','\n');
%arrange them in order of k if you want in a cell array
FinalList{k,1}=List;
%or print them into a file.
fprintf(fid, [cell2mat(List) '\n']);
end
%close file indentifier
fclose(fid)
Yeswanth Kaushik
el 8 de Jun. de 2023
try writetable or writecell commands.
Respuesta aceptada
Más respuestas (3)
WAEL Al-dulaimi
el 8 de Dic. de 2017
1 voto
In case I want to open a text file and I have a loop that each time give me different results {x1, x1...xn}. So how can I save all the data that I got from the loop in different names such as y1, y2 .... etc? And under each one of them, I can find the result that I got. I'll appreciate your help. thank you
1 comentario
Peter
el 8 de Dic. de 2017
It is unclear form your question what you are trying to loop across. It sounds like your trying to read from a single text file; is this true? In any case you should be able to just assign the value that you read to a new variable. If you know how long each data set is you can preallocate matrices for those variables, but that's not necessarily necessary.
Ahmed Saeed Mansour
el 20 de En. de 2019
1 voto
How can we save transient CFD results in a file during running the code?
1 comentario
Venkatanarasimha Hegde
el 17 de Mzo. de 2023
Its better if you store the time data at different instants along another matrix dimension (possible in C not sure about matlab) or you can concatinate the new data to existing file, so while visualising you can access after the end index of each time instant.
Paige Kierce
el 31 de Mayo de 2020
1 voto
randA
Categorías
Más información sobre Language Support en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!