Hi, I would like to know how can I write a column into a .txt file.
Let's say that the output is
A = [0.045; 0.011; 0.003; 0.005]*1.0e-03
I would also like to know how instead I can add the output to an already existing text file as a column, the last column in this case.
Summing up:
  • How to write output in new file as a column
  • How to write output in an existing file as the last column

 採用された回答

Star Strider
Star Strider 2019 年 4 月 5 日
編集済み: Star Strider 2019 年 4 月 5 日

1 投票

You have several options.
One approach:
fid = fopen('YourFile.txt,'wt');
fprintf(fid,'%.6f\n',A)
fclose(fid);
Experiment to get the result you want.
EDIT —
You edited your Question, so I’m editing my Answer.
To append the column to the end of an existing file using the dlmread (link) function (consider using the readmatrix (link) function if you have R2019a or later), to read the file into a matrix of the same row size as ‘A’, or truncate ‘A’ or append NaN values to ‘A’ as appropriate to make the row sizes equal, then concatenate it to the end of your matrix. Then write it to a new or existing file name.
The concatenation would be:
NewMMatrix = [OldMatrix A];
The easiest way to write this would be to use the dlmwrite (link) function, or if you have R2019a or later, the writematrix (link) function.

7 件のコメント

Marisabel Gonzalez
Marisabel Gonzalez 2019 年 4 月 5 日
Thanks Star Strider! Would this also work for an already existing file? i.e. would it add an extra column at the end of that file with my output?
Marisabel Gonzalez
Marisabel Gonzalez 2019 年 4 月 5 日
Would you mind writing that in code? I am a bit confused...
Star Strider
Star Strider 2019 年 4 月 5 日
Thanks Star Strider! Would this also work for an already existing file? i.e. would it add an extra column at the end of that file with my output?
See the edited part of my Answer.
Would you mind writing that in code? I am a bit confused...
Example code would be:
OldMatrix = readmatrix('YourFile.txt');
rwsz = size(OldMatrix,1);
if numel(A) < rwsz
NewA = nan(rwsz,1);
NewA(1:numel(A)) = A;
else
NewA = A(1:rwsz);
end
NewMatrix = [OldMatrix, NewA];
writematrix(NewMatrix, 'YouNewFile.txt')
This is partially tested, since I didn’t create or write the files. The rest works.
Experiment to get the result you want.
Marisabel Gonzalez
Marisabel Gonzalez 2019 年 4 月 5 日
編集済み: Marisabel Gonzalez 2019 年 4 月 5 日
for dlmread/dlmwrite it does not work =(
I get the following error:
Error using dlmwrite (line 79)
FILENAME must be a character vector.
Star Strider
Star Strider 2019 年 4 月 5 日
編集済み: Star Strider 2019 年 4 月 5 日
FILENAME must be a character vector.
Provide a character array for it. My example code shows how to do that. A character vector is defined as being within single quotes ('), such as 'YourFile.txt' and 'YouNewFile.txt'. See the documentation section on Characters and Strings (link) for a full discussion.
EDIT —
If this is your code:
A = [0.23;0.44;0.65;0.12];
A = A';
oldfile = dlmread('testfile1.txt');
oldfile = oldfile';
dlmwrite(oldfile,A,'-append','newline','pc','delimiter',' ')
you only need to do a slight re-write to do what you want:
A = [0.23;0.44;0.65;0.12];
A = A';
file_name = 'testfile1.txt';
oldfile = dlmread(file_name);
oldfile = oldfile';
dlmwrite(file_name,oldfile,A,'-append','newline','pc','delimiter',' ')
If my Answer helps you solve your problem, please Accept it!
Marisabel Gonzalez
Marisabel Gonzalez 2019 年 4 月 5 日
I still haven't got it to work (feeling close) but your answer has been useful!
Star Strider
Star Strider 2019 年 4 月 5 日
Thank you.

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by