snakile
snakile

Reputation: 54561

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

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?

Upvotes: 13

Views: 26045

Answers (3)

pablo.vix
pablo.vix

Reputation: 2233

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');

Upvotes: 0

snakile
snakile

Reputation: 54561

To write myMatrix to myFile.txt:

dlmwrite('myFile.txt', myMatrix);

To read the file into a new matrix:

newMatrix = dlmread('myFile.txt');

Upvotes: 21

Ghaul
Ghaul

Reputation: 3330

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

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

Upvotes: 2

Related Questions