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

2 views (last 30 days)
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
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é
René on 9 Oct 2014
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)

Geoff Hayes
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é
René on 13 Oct 2014
Edited: René on 13 Oct 2014
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...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!