How do you run a script with a button in a function?
8 views (last 30 days)
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
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)
See Also
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!