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

前端 未结 3 1007
轮回少年
轮回少年 2020-12-28 08:51

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


        
相关标签:
3条回答
  • 2020-12-28 09:32

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

    >> myMatrix2 = double(myMatrix);
    >> save myFile.txt myMatrix2 -ASCII
    
    0 讨论(0)
  • 2020-12-28 09:32

    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');
    
    0 讨论(0)
  • 2020-12-28 09:48

    To write myMatrix to myFile.txt:

    dlmwrite('myFile.txt', myMatrix);
    

    To read the file into a new matrix:

    newMatrix = dlmread('myFile.txt');
    
    0 讨论(0)
提交回复
热议问题