How to run a for loop one at a time with each button click?

14 Ansichten (letzte 30 Tage)
Keerthana Natarajan
Keerthana Natarajan am 8 Nov. 2022
I have a for loop within a uicontrol-defined button. The for loop has to run 3 times, however it runs all the three loops together when the button is pushed once. How can I run the loop one at a time with every button click?
I have the chunk of code pasted here
Thanks!
if buttonA.Value==1
for i=1:3
r1(i)=randi(length(gettrialone));
r2(i)=randi(length(paytrialone));
set(messaget1g1,'String',sprintf('Collect $%d and pay a fine of $%d',...
[gettrialone(r1(i)),paytrialone(r2(i))]))
get(buttonA,'Enable');set(buttonA,'Enable','off');
get(buttonB,'Value');set(buttonB,'Enable','off');
datat1g1.Button_Pressed(i)='A'; %Data collection but need for loop
datat1g1.Earned(i)=gettrialone(r1(i)); %Data collection but need for loop
datat1g1.Fee(i)=paytrialone(r2(i)); %Data collection but need for loop
end
end

Antworten (1)

Jim Riggs
Jim Riggs am 8 Nov. 2022
At the end of your "for" loop, add a "while" loop that looks for the button value.
if i < 3 (you don't want it to pause the last time)
while ~button_status
(get button_status) % I don't know the exact commands for this
end
end
This will cause it to loop indefinitely until the button is pressed.
Make sure that the button status is reset inside the for loop.
  1 Kommentar
Keerthana Natarajan
Keerthana Natarajan am 9 Nov. 2022
Maybe I interpreted your answer wrong but it is breaking the code (no error popping up but the code is not progressing).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by