Set function workspace to base in MATLAB


Set function workspace to base in MATLAB



I have a rather bulky program that I've been running as a script from the MATLAB command line. I decided to clean it up a bit with some nested functions (I need to keep everything in one file), but in order for that to work it required me to also make the program itself a function. As a result, the program no longer runs in the base workspace like it did when it was a script. This means I no longer have access to the dozens of useful variables that used to remain after the program runs, which are important for extra calculations and information about the run.



The suggested workarounds I can find are to use assignin, evalin, define the variables as global, or set the output in the definition of the now function-ized program. None of these solutions appeal to me, however, and I would really like to find a way to force the workspace itself to base. Does any such workaround exist? Or is there any other way to do this that doesn't require me to manually define or label each specific variable I want to get out of the function?


assignin


evalin





one workaround would be to run save('workspace'); at the end of your function. Then after it runs you can run load('workspace'); from the command line to see all the variables
– Trogdor
Aug 6 '14 at 19:47



save('workspace');


load('workspace');





This is definitely the easiest way to do what I want as far as I can see so far. Thanks Trogdor!
– A Blue Shoe
Aug 6 '14 at 22:13





If you also want the variables to be assigned after an error, try adding void = onCleanup(@variables2workspace); at the beginning of your function, where variables2workspace can be a function from any of these answers. It is executed when void is destroyed, i.e., at the end of the function or any error.
– Scz
Dec 3 '15 at 16:24



void = onCleanup(@variables2workspace);


variables2workspace


void




2 Answers
2



Functions should define clearly input and output variables. Organizing the code differently will be much more difficult to understand and to modify later on. In the end, it will most likely cost you more time to work with an unorthodox style than investing in some restructuring.



If you have a huge number of output variables, I would suggest organizing them in structure arrays, which might be easy to handle as output variables.



The only untidy workaround I can imagine would use whos, assignin and eval:


function your_function()
x = 'hello' ;
y = 'world' ;

variables = whos ;
for k=1:length(variables)
assignin('base',variables(k).name,eval(variables(k).name))
end
end



But I doubt that this will help with the aim to clean up your program. As mentioned above I suggest ordering things manually in structures:


function out = your_function()
x = 'hello' ;
y = 'world' ;

out.x = x ;
out.y = y ;
end





Ahhh eval, pleas don't!
– Cape Code
Aug 6 '14 at 20:21


eval





Could I get an explanation on why the eval method is no good? It appears to be the method with the least amount of work involved on my part at first glance.
– A Blue Shoe
Aug 6 '14 at 21:32


eval





eval is evaluated in runtime. There are a lot of reasons why this is bad. For examlpe compiler cannot find errors.
– patrik
Aug 6 '14 at 22:11


eval





Great answer! I have to run my scripts as functions, because I need sub-functions (and that's not possible with scripts). But when the function ends, all variables go out of scope and vanish. With your solution, I can run my function-wrapped scripts like scripts again. :)
– Edgar
Sep 11 '15 at 9:56



If the function you would like to define are simple and have a single output, one option is to use anonymous functions.



Another option is to store all the variable you would like to use afterwards in a struct and have your big function return this struct as an output.


struct


struct


function AllVariables = GlobalFunction(varargin);
% bunch of stuff

AllVariables= struct('Variable1', Variable1, 'Variable2', Variable2, …);
end






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux