writing maxtrix into a txt file
2 次查看(过去 30 天)
显示 更早的评论
I am trying to write a maxtrix into a txt file which contains other information, so i am wondering how i would go about this? could i use dlmwrite, will this write everything in the same file as my other information? Thanks.
p.s. right now i am writing every number in one by one with a nested loop, takes a long time to do since i have a 24000X103 matrix, and also takes forever to open.
0 个评论
采纳的回答
Walter Roberson
2012-1-17
dlmwrite() cannot handle a mix of text and numbers. It can handle numbers alone, and it can handle text alone (when backed in to a corner.)
Are you just trying to append a matrix to an existing file? If so then:
thisfmt = [repmat('%g ', 1, size(YourMatrix,2)-1),, '%g\n'];
fid = fopen('YourExistingFile.txt', 'at');
printf(fid, thisfmt, YourMatrix .'); %transpose is critical!
fclose(fid)
Adjust the %g to whatever is appropriate.
2 个评论
Walter Roberson
2012-1-17
The above code will write 24000 rows each containing 103 columns -- "row major order".
Did you want to write by columns across rather than by rows?
thisfmt = [repmat('%g ', 1, size(YourMatrix,1)-1),, '%g\n'];
fid = fopen('YourExistingFile.txt', 'at');
printf(fid, thisfmt, YourMatrix); %NO transpose!
fclose(fid)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!