writing a matrix into a file

조회 수: 9 (최근 30일)
Tina
Tina 2013년 3월 29일
댓글: Aman Kumar 2020년 11월 3일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 29일
편집: Azzi Abdelmalek 2013년 3월 29일
dlmwrite('file.txt',A)
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 29일
편집: Azzi Abdelmalek 2013년 3월 29일
Use
dlmwrite('file.txt',A,'newline','pc')
Aman Kumar
Aman Kumar 2020년 11월 3일
Just take transpose of your matrix and then write it to a file

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

the cyclist
the cyclist 2013년 3월 29일
편집: the cyclist 2013년 3월 29일
There are many possible ways. One is to use the frprintf() function. See
doc fprintf
for syntax and examples.
  댓글 수: 1
Tina
Tina 2013년 3월 29일
I know, but I could not figure out how I can have the file such that each line has only one number in it.

댓글을 달려면 로그인하십시오.


Susan
Susan 2013년 3월 29일
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
James Tursa
James Tursa 2017년 10월 19일
@Eric: Please delete this comment and instead open up a new question with your code.

댓글을 달려면 로그인하십시오.


Eric Berger
Eric Berger 2017년 10월 19일
I'm going to try to debug on my own, I can delete this if you want

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by