How do you run a script with a button in a function?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Rick van den Hadelkamp
le 25 Juil 2017
Modifié(e) : Rick van den Hadelkamp
le 25 Juil 2017
Stephen23
le 25 Juil 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.
Réponses (0)
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!