MATLAB: Hi all :how to save some new variables to different mat file

save new variables separately

I have a code written to create some variables. At the beginning of the code, it loads the file "ws0722.mat" file. because of the "ws0722.mat" contains some variables which are used inside the new code to create new variables. how can I only save the new variables to new mat file, save function doesn't work for me as I have more (> 15) variables to name e:g save ('ws0722_B','s','a') if anybody can help,much appreciated Thanks

Best Answer

Before the load, use
existing_names = {};
existing_names = who();
then do your load and so on.
When you are ready to save, do
new_names = setdiff(who(), existing_names);
save(('ws0722_B', new_names{:});