Save all workspace within a function

36 次查看(过去 30 天)
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!
  3 个评论
Baham91
Baham91 2015-4-19
The point is that I have a function which does several tasks that may take lots of hours. I can not split it into two or more functions and I would like to save, somehow, the progress. Saving the workspace and setting some variables to global seemed to me like the only solution, but maybe there is a better one!
Adam
Adam 2015-4-20
I see you accepted Geoff's answer so I assume that achieved what you want, although the exact nature of what you are aiming for still seems a little unclear. As far as I understand it, using that approach will save the workspace variables to file, but those workspace variables will be exactly the same as they were at the point you called the function unless you are using OOP with handle-derived classes or doing some ghastly assignin type behaviour within your function.
I would have thought that the progress which you mention would be encapsulated within your function, not in the base workspace.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2015-4-18
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by