GUI with nested functions: Writing Values to Base-Workspace and running a Script afterwards

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

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?
Yes, that ist exactly my problem! I 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. :-) Maybe, there is a good reason for Matlab to block this possibility...
But thats what i need and now i have to know some workaround to do so. I cant change a bit on the script, i just have to feed the script trough the GUI with variables. How can this be done?

Sign in to comment.

Answers (1)

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

Okay, i get that is the clean way to do it: Turn the script in to a Function and pass the variables bei Reference.
But as i said, i am not "allowed" to change a bit on the script, because it needs about 15 parameters and Vectors, it is foreign, huge, calls many subscripts which call many sub-functions. My Interface to the script is the clean workspace with only the variables in it.
You will know rise your eyebrow or moan about this: Is there a way to "break" the static Workspace? Or to Copy the Values from the static Workspace to the "real" Base Workspace? I tried with save , clear and load , but already the save Command doesnt work from the function.
save and load
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;
Thats what i did in the first place. But the script later uses the workspace and attempt to write new variables.
Attempt to add "test" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for
details.
Error in Bedatung_Batch (line 18)
test='bla';
Error in run (line 63)
evalin('caller', [script ';']);
Error in can_scan_gui/pbRun (line 299)
run Bedatung_Batch
Error while evaluating uicontrol Callback
The "static workspace" is the problem. I wish to remove all restrictions from the workpace and use it as if the variables loaded from a file, not from a GUI functions. I cant believe that i am the only one with this problem. For me, this is a basic GUI functionality: transfer variables to the workspace. I dont understand why matlab dont want to let me do this. I also accept a "dirty" trick to cheat the static workspace. ;-)
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.
I read it many times and i understand that the feature i want is bad and ugly and stupid and mathworks want me to save from trouble. ;-)
But as i said, the script is untouchable and the Workspace is the only interface. My wish stays stable and i want to break this cage around the static workspace. Grrr! ;-)
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.
Okay then. At least, saving variables from inside the function to a file and then close, clear and load the variables would do the job. But already the "save" command isnt working:
Warning: com.mathworks.hg.peer.HG1FigurePeer@7f7d457a is not serializable
> In scan_gui at 272
Error using save
Can't write file
D:\Users\bla\Documents\MATLAB\SCAN_TOOL_GUI\test.mat.
Error in scan_gui (line 272)
save test
Im sure, there is also a good reason to prohibit this possibility... ;-)
What are you calling 'save' on? You will likely need to specify explicitly the variables to save to file as you don't want the handles for GUI components saved to file, just your actual data.
The save-command without options just write all workspace variables to the file - in a script. Now i see that this command in a function needs explicitly variables as argument. And works only with variables, not structs. Ahhh! Thanks for your answer!
I am crawling slowly forward...
Structs are still variables, you should be able to save these like any other variable.

Sign in to comment.

Categories

Asked:

on 9 Oct 2014

Commented:

on 13 Oct 2014

Community Treasure Hunt

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

Start Hunting!