blue-sky
blue-sky

Reputation: 53826

.mat file variable is overridden with save

I have a matlab file which contains these values :

enter image description here

Opening the file, here a snippet of X :

enter image description here

And y :

enter image description here

Deleting all rows in y and saving, need to replace the current file :

enter image description here

Replacing the file causes all of the X values to be removed also :

enter image description here

Can see from above the variable 'y' is no longer present.

How can edit a .mat file 'y' variable without removing the 'X' variable ?

Upvotes: 0

Views: 508

Answers (2)

TroyHaskin
TroyHaskin

Reputation: 8401

If you have Matlab R2011b of later, you can also use the matfile function to get a dynamic handle to the data stored in the MAT-file. This is typically reserved for large files with data that should only to be loaded into memory when needed, but the functionality is similar to using save and more interactive. For your current example:

x = rand(5000,400);
y = rand(5000,1);
save('data.mat','x','y');
m = matfile('data.mat','Writable',true);
m.y = [];

And y in the MAT-file is automatically updated.

Upvotes: 1

Ander Biguri
Ander Biguri

Reputation: 35525

Use the '-append' option when saving.

Doing save('data.mat','x','-append') or save data.mat x -append will either append the data or substitute the variable without modifying the rest of the data.

Upvotes: 2

Related Questions