Azzera filtri
Azzera filtri

Save data to .txt file

2.027 visualizzazioni (ultimi 30 giorni)
Bernoulli Lizard
Bernoulli Lizard il 5 Set 2012
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 an nx1 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 Commenti
Raghunandan V
Raghunandan V il 4 Ott 2018
Modificato: per isakson il 20 Gen 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
for k=1:length(fileName)
%read the file name as string including delimiters and next lines
%arrange them in order of k if you want in a cell array
%or print them into a file.
fprintf(fid, [cell2mat(List) '\n']);
%close file indentifier
Yeswanth Kaushik
Yeswanth Kaushik il 8 Giu 2023
try writetable or writecell commands.

Accedi per commentare.

Risposta accettata

José-Luis il 5 Set 2012
Modificato: MathWorks Support Team il 4 Nov 2020
To write a text file with columns of data that have variable names, use the “writetable” function. First, create the data to write, put it in a table with variable names, and then write the data to text file.
% Create two columns of data
A = rand(10,1);
B = rand(10,1);
% Create a table with the data and variable names
T = table(A, B, 'VariableNames', { 'A', 'B'} )
% Write data to text file
writetable(T, 'MyFile.txt')
For more information see:
Starting in R2019a, you can use "writematrix" to write a matrix to a file.
For example:
writematrix(M, "M.txt");
For more information on "writematrix", please refer to the following documentation:
  13 Commenti
lafnath p
lafnath p il 14 Nov 2016
how to get the size of data in file
celine azizieh
celine azizieh il 14 Ago 2017
Better to use: fid=fopen('MyFile.txt','wt');
instead of fid=fopen('MyFile.txt','w');
otherwise when opening the file with notepad, there is not return to the line... everything is written on one line.

Accedi per commentare.

Più risposte (3)

WAEL Al-dulaimi
WAEL Al-dulaimi il 8 Dic 2017
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 Commento
Peter il 8 Dic 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.

Accedi per commentare.

Ahmed Saeed Mansour
Ahmed Saeed Mansour il 20 Gen 2019
How can we save transient CFD results in a file during running the code?
  1 Commento
Venkatanarasimha Hegde
Venkatanarasimha Hegde il 17 Mar 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.

Accedi per commentare.

Paige Kierce
Paige Kierce il 31 Mag 2020


Scopri di più su Data Import and Export in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by