MATLAB: Save all workspace within a function

saveworkspace

Hello everybody! I'm trying to save the all workspace using the function save within a function. Unfortunately, if I do that, it saves only the local variables of the function, while I would like to save the global workspace. Is there a way to do that, which is not declare all variables as global variables?
Thank you!

Best Answer

Baham91 - if it is absolutely necessary to save the variables from the base workspace from within your function, then you can use the evalin function to do so. For example, you would do
evalin('base','save baseVars.mat');
which will evaluate the command
save baseVars.mat
in the base workspace, and so will save all variables from there into the file baseVars.mat.
There is some concern about using this function, so be wary and only use it if you are convinced that you need to do so.