writing a matrix into a file
10 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Azzi Abdelmalek
2013-3-29
编辑:Azzi Abdelmalek
2013-3-29
dlmwrite('file.txt',A)
4 个评论
更多回答(3 个)
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.
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
2017-10-19
@Eric: Please delete this comment and instead open up a new question with your code.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!