How do you run a script with a button in a function?
Show older comments
I have a (simplified) script called Holecheck. This script contains an important variable 'I':
if
%code
else if
%code
else
run Doesnotfit
end
end
Doesnotfit is a function looking as follows:
function Doesnotfit
I=evalin('base','I');
%stop=evalin('base','stop');
% I = 0;
%fgh = figure('position',[1100,30,210,60]);
uicontrol('Parent',figure(2), 'Style', 'Pushbutton', 'String', 'Go left',...
'Position',[0.1,0.1,50,20], 'Callback', @Ileft);
uicontrol('Parent',figure(2), 'Style', 'Pushbutton', 'String', 'Go right',...
'Position',[100,0.1,50,20], 'Callback', @Iright);
uicontrol('Parent',figure(2), 'Style', 'Pushbutton', 'String', 'Accept',...
'Position',[50,0.1,50,20], 'Callback', @Iaccept);
mTextBox = uicontrol('Parent',figure(2),'style','text');
set(mTextBox,'String',num2str(I))
function Ileft(~,~)
I=I-1
assignin('base', 'I', I)
set(mTextBox,'String',num2str(I))
end
function Iright(~,~)
I=I+1
assignin('base', 'I', I)
set(mTextBox,'String',num2str(I))
end
function Iaccept(~,~)
Holecheck
end
end
In short, there are three buttons and a text. The first two buttons add 1 or -1 to variable 'I'. When last button (Iaccept) is pressed, the script should automatically run the starting file 'Holecheck' again from the start. However, I get the following error:
Attempt to add "Xout" to a static workspace. See Variables in Nested and Anonymous Functions.
Error in Holecheck (line 2) Xout=500;
Error in Doesnotfit/Iaccept (line 31) Holecheck
Error while evaluating UIControl Callback
I have already read something about 'Variables in Nested and Anonymous Functions' but i still don't understand.
Please can you help me? Thanks
3 Comments
Why is Holecheck a script rather than a function? Also it would be so much simpler if you just pass variables around as arguments to functions rather than using evalin and assignin. Functions each have their own workspace into which you can pass arguments and return them rather than trying to keep forcing them in and out of the base workspace which shouldn't be involved at all.
You get the error because, as a script, Holecheck (whose troublesome line you haven't included in the above code anyway) will just dump all its variables into the calling workspace, except you cannot do that because the workspace is static because it contains nested functions therefore variables cannot be added to it. If it were a function then it wouldn't have this problem unless you return arguments from it and in that scenario it could be worked around by pre-declaring the variable in question I would imagine.
Rick van den Hadelkamp
on 25 Jul 2017
Edited: Rick van den Hadelkamp
on 25 Jul 2017
Stephen23
on 25 Jul 2017
"Do you think it is easy to convert this script into a funtion?"
Not just easy but highly recommended. For many reasons functions are just much better to work with: search this forum for the many threads that discuss this.
Answers (0)
Categories
Find more on Entering Commands 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!