How to save a matrix as text file?
52 次查看(过去 30 天)
显示 更早的评论
I want to save a matrix as text file.
Each column should be separated by tab.
The output file should be read with any text editor
When the output is opened, it should display the numbers
in the same way it looks like in Matlab.
Thank you for your help
Emerson
0 个评论
采纳的回答
Matt Fig
2011-3-28
One way is to use FPRINTF.
A = round(rand(6,7)*9) % Write this to file.
fid = fopen('Mymatrix.txt','wt');
for ii = 1:size(A,1)
fprintf(fid,'%g\t',A(ii,:));
fprintf(fid,'\n');
end
fclose(fid)
EDIT
Changed the 'w+' to 'wt' in the FOPEN call.
If you have floating point numbers, you may want to use '%20.18f \t' instead of '%g\t' or similar. See FPRINTF for format specifiers.
0 个评论
更多回答(3 个)
Walter Roberson
2017-12-16
You could also consider dlmwrite telling it to use \t as the delimiter.
You could also consider using
save('MyMatrix.txt', 'A', '-double', '-tab')
Where A is the name of the variable
3 个评论
Walter Roberson
2019-2-5
double precision. Without the -double only about 7 digits are written out, about as much as needed to reproduce single precision numbers.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!