GUI with nested functions: Writing Values to Base-Workspace and running a Script afterwards
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?
René
on 9 Oct 2014
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
René
on 9 Oct 2014
Geoff Hayes
on 9 Oct 2014
What about calling your script from within the function, so that the workspace to the script is the workspace of the calling function?
function myFunc(myVar1,myVar2,myVar3)
% call myScript that makes use of the input variables myVar1,myVar2,
% and myVar3
myScript;
René
on 10 Oct 2014
Geoff Hayes
on 10 Oct 2014
It may be that developers are moving away from writing data to the base workspace. If you go back to the link Variables in Nested and Anonymous Functions, the table shows that for the type of operation of eval, evalin, or assignin, the best practice column states If possible, avoid using these functions altogether.
Jan
on 11 Oct 2014
You understand that it is an ugly method but the script is untouchable. Well.
For me, this is a basic GUI functionality: transfer variables to the workspace.
No, this is exactly not a basic GUI functionality. The idea of remote-controlled injection of variables ibn otehr workspaces are instable and ugly. As soon as two GUIs are open, a well-defined interaction is impossible because it depends on the timing in a critical way.
With assignin/evalin you will and must get into troubles, and you do, as you see. Tweaking and dirty tricks will increase the troubles, so it is a bad idea to try to solve the problems in this way. Even if it might work currently, it will fail for other Matlab versions or if the program is modified anywhere else.
Do not break the cage, but use the stable and clear methods to share data between functions. If you have an ugly script, cover it by a function which catchs inputs and outputs in a clear way.
René
on 13 Oct 2014
Adam
on 13 Oct 2014
Structs are still variables, you should be able to save these like any other variable.
Categories
Find more on Structures 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!