How do I save a matrix of integers to a text file in Matlab?

久未见 提交于 2019-12-18 11:18:11

问题


I have a 2D matrix myMatrix of integers which I want to save its content to a text file. I did the following:

save myFile.txt myMatrix -ASCII

I get this message:

Warning: Attempt to write an unsupported data type to an ASCII file. Variable 'myMatrix' not written to file. and nothing is written.

What to do?


回答1:


To write myMatrix to myFile.txt:

dlmwrite('myFile.txt', myMatrix);

To read the file into a new matrix:

newMatrix = dlmread('myFile.txt');



回答2:


You have to convert your matrix to double before using save.

>> myMatrix2 = double(myMatrix);
>> save myFile.txt myMatrix2 -ASCII



回答3:


Building on snakile's earlier answer: to write myMatrix to myFile.txt, using CR/LF as line terminator ('pc'), otherwise, you should use LF ('unix'):

dlmwrite('myFile.txt', myMatrix,'newline','pc');

To read the file into a new matrix:

newMatrix = dlmread('myFile.txt');


来源:https://stackoverflow.com/questions/3546756/how-do-i-save-a-matrix-of-integers-to-a-text-file-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!