Reputation: 53826
I have a matlab file which contains these values :
Opening the file, here a snippet of X :
And y :
Deleting all rows in y and saving, need to replace the current file :
Replacing the file causes all of the X values to be removed also :
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
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
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