MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Save data to .txt file

Bernoulli Lizard さんによって質問されました 2012 年 9 月 5 日
最新アクティビティ per isakson
さんによって 編集されました 2019 年 1 月 20 日
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.
thanks

  1 件のコメント

Raghunandan V 2018 年 10 月 4 日
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)

サインイン to comment.

3 件の回答

José-Luis
回答者: José-Luis
2012 年 9 月 5 日
編集済み: MathWorks Support Team 2018 年 11 月 9 日
 採用された回答

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:

  13 件のコメント

swati khan 2016 年 11 月 9 日
% code
A = rand(10,1);
B = rand(10,1);
header1 = 'Hello';
header2 = 'World!';
fid=fopen('MyFile.txt','w');
fprintf(fid, [ header1 ' ' header2 'r\n']);
fprintf(fid, '%f %f r\n', [A B]');
fclose(fid);true
% code
Thanks for the code. Was looking for something exactly like this. May I add that "If you plan to read the file with Microsoft® Notepad, use '\r\n' instead of '\n' to move to a new line." https://www.mathworks.com/help/matlab/ref/fprintf.html
lafnath p 2016 年 11 月 14 日
how to get the size of data in file
celine azizieh 2017 年 8 月 14 日
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.

サインイン to comment.


回答者: WAEL Al-dulaimi 2017 年 12 月 8 日

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 件のコメント

Peter
2017 年 12 月 8 日
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.

サインイン to comment.


回答者: Ahmed Saeed Mansour 2019 年 1 月 20 日

How can we save transient CFD results in a file during running the code?

  0 件のコメント

サインイン to comment.



Translated by