GUI with nested functions: Writing Values to Base-Workspace and running a Script afterwards
2 views (last 30 days)
Show older comments
I am developing a GUI for an existing script, which expect some Struct Values in the Base Workspace. The GUI shall only accept the User Values, transmit it to the Base-Workspace and then start the script und close itself immediately. It sounds very simply and the values going with assignin and evalin but i work for hours with the Problem that if the script create afterwards values itself, i get the Message:
Attempt to add var_name to a static workspace. See MATLAB Programming, Restrictions on Assigning to Variables for details.
What is the Problem? The GUI is closed and the Script shall use the Base-Workspace without restrictions. How can this be done?
2 Comments
Geoff Hayes
on 9 Oct 2014
René - what is the line (or lines) of code that is generating the error message? Have you looked at Variables in Nested and Anonymous Functions to see if that is applicable to your problem?
Answers (1)
Geoff Hayes
on 9 Oct 2014
René - since you just want write a variable from a nested Function (because of the User Input) an then run a foreign Script that works on this variables and Matlab doesnt let me do it, then why not just change your script into a function, and pass those variables (that you would have written to the workspace) as input parameters into the function.
Suppose your script is named myScript.m and all it contains is a number of lines of code that, for example, makes use of a variable named myVar that has been declared in the workspace
close all; % just dummy code
fprintf('the value of myVar is %f\n',myVar);
Now rather than reading this variable from the workspace, just change your script to a function and pass myVar as an input
function myScript(myVar)
close all;
fprintf('the value of myVar is %f\n',myVar);
In your GUI, rather than doing something like
assignin('base','myVar',myVar);
myScript;
you would just replace these two lines with
myScript(myVar);
It may not be as simple as that; your function may have to return some variables that normally would have been written to the workspace, but perhaps you can start from here.
10 Comments
Adam
on 13 Oct 2014
Structs are still variables, you should be able to save these like any other variable.
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!