writing a matrix into a file

Hello;
I have a matrix A, let's say it is a 100x1 matrix. I want to put the data into a text file, such that each line corresponds to the row of my matrix. How can I do it?

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 29 de Mzo. de 2013
Editada: Azzi Abdelmalek el 29 de Mzo. de 2013

1 voto

dlmwrite('file.txt',A)

4 comentarios

Tina
Tina el 29 de Mzo. de 2013
This just puts the numbers of the matrix right next to eachother. I want the file such that each line has only one number in it. How can I do it?
Azzi Abdelmalek
Azzi Abdelmalek el 29 de Mzo. de 2013
That depends on how you open your txt file, with wordpad you will get one column, with notepad you will get one line
Azzi Abdelmalek
Azzi Abdelmalek el 29 de Mzo. de 2013
Editada: Azzi Abdelmalek el 29 de Mzo. de 2013
Use
dlmwrite('file.txt',A,'newline','pc')
Aman Kumar
Aman Kumar el 3 de Nov. de 2020
Just take transpose of your matrix and then write it to a file

Iniciar sesión para comentar.

Más respuestas (3)

the cyclist
the cyclist el 29 de Mzo. de 2013
Editada: the cyclist el 29 de Mzo. de 2013

0 votos

There are many possible ways. One is to use the frprintf() function. See
doc fprintf
for syntax and examples.

1 comentario

Tina
Tina el 29 de Mzo. de 2013
I know, but I could not figure out how I can have the file such that each line has only one number in it.

Iniciar sesión para comentar.

Susan
Susan el 29 de Mzo. de 2013

0 votos

Here's a basic script that you should be able to modify and get to work using fprintf. Look at the help document for other things you can specify, like field width.
homedir = cd; % Save current directory as home directory
nrows = size(A,1);
flnm = 'Matrix A'; % Specify string for filename
cd(newdir); % Change directory to new directory, if desired. Must specify string for new directory.
fid = fopen(flnm,'wt+'); % Create writable textfile
% Enter data into text file
for i = 1:nrows
fprintf(fid,'%f\r\n',A(i,:)); % The %f is for floating point numbers, or you could use %d for double-precision integers
end
% Close text file and return to home directory
fclose(fid);
cd(homedir); % Return to home directory

1 comentario

James Tursa
James Tursa el 19 de Oct. de 2017
@Eric: Please delete this comment and instead open up a new question with your code.

Iniciar sesión para comentar.

Eric Berger
Eric Berger el 19 de Oct. de 2017

0 votos

I'm going to try to debug on my own, I can delete this if you want

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Mzo. de 2013

Comentada:

el 3 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by