There are a couple good ways to do this. If your MAT-file has a lot of data stored in it, save using the -append flag. Another approach, particularly if you need to modify more than one of the struct arrays stored in the MAT-file, is to load into a struct and save that struct using the -struct flag.
data = load('myfile.mat');
data.struct1.newfield = 42;
save('myfile.mat', '-struct', 'data')
With this approach you don't need to worry about overwriting a variable in your workspace that happens to have the same name as one of the variables in the MAT-file.
2 Comments
Geoff Hayes (view profile)
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/348392-save-new-field-to-a-structure#comment_468540
Adam (view profile)
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/348392-save-new-field-to-a-structure#comment_468541
Sign in to comment.