mysticsasuke
mysticsasuke

Reputation: 119

Saving matrix values in MATLAB

I saved matrix values onto a .mat file for later use. The matrix size is 5.6kx3.4k. The values inside the matrix range from 0-10k. When I saved the matrix using - save(x.mat), the size was around 80MB. However, when I equated the same matrix to another matrix b = x; and then saved that matrix as a .mat file, the size increases, sometimes by a large margin. Why does this happen?

Also, am I doing something wrong in the way I am saving these matrices?

Upvotes: 0

Views: 288

Answers (1)

Chris Taylor
Chris Taylor

Reputation: 47402

The (equivalent) commands

save('x.mat')
save x.mat

tell MATLAB to save the entire contents of the workspace in a file called "x.mat" - so if you do

>> x = randn(5000, 100);
>> save('x.mat');

you will get a file of some size, and if you then do

>> b = x;
>> save('x.mat');

your file will roughly double in size, because you now have two matrices (x and b) being saved in it.

If you want to save a particular variable, you can do

>> save('x.mat', 'x');
>> save('b.mat', 'b');

which will create two files "x.mat" and "b.mat" which contain x and b respectively. Of course, there is no requirement that the file names match the variable names - doing

>> save('unicorn.mat', 'x');
>> save('apricot.mat', 'b');

which will create two files "unicorn.mat" and "apricot.mat" which contain x and b respectively.

Upvotes: 2

Related Questions