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

 Respuesta aceptada

Star Strider
Star Strider el 5 de Abr. de 2019
Editada: Star Strider el 5 de Abr. de 2019

1 voto

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 comentarios

Marisabel Gonzalez
Marisabel Gonzalez el 5 de Abr. de 2019
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 el 5 de Abr. de 2019
Would you mind writing that in code? I am a bit confused...
Star Strider
Star Strider el 5 de Abr. de 2019
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 el 5 de Abr. de 2019
Editada: Marisabel Gonzalez el 5 de Abr. de 2019
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 el 5 de Abr. de 2019
Editada: Star Strider el 5 de Abr. de 2019
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 el 5 de Abr. de 2019
I still haven't got it to work (feeling close) but your answer has been useful!
Star Strider
Star Strider el 5 de Abr. de 2019
Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2018a

Preguntada:

el 5 de Abr. de 2019

Comentada:

el 5 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by