callback code for push_button in matlab GUI to stop fast iterations due to FOR loops in main program

I want to interrupt or break the program iterations (due to FOR loops in main program) in a GUI using a push_button. Can any one give me the precise pushbutton_callback code for the same. Your help will be greatly appreciated.
Thanks, K.D.Singh

 Accepted Answer

function pushbutton_callback(hObject, eventdata, handles)
setappdata(0 , 'Flag', 0);
while your other script might be like this
setappdata(0 , 'Flag' , 1);
while(1)
sprintf('in loop')
Flag=getappdata(0,'Flag');
if Flag == 0
break;
end
drawnow
end

9 Comments

Dear Dishant, Thanks for your suggestion, but I am not getting where I have to put these scripts? Although I have used the same in my STOP push-button callback function (see below), but as I am pressing this STOP push-button my program's for loop iterations are not breaking and running continuously. Please let me know your idea? Thanks for your time...
function pushbutton_Callback(hObject, eventdata, handles) % hObject handle to pushbutton9 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) setappdata(0 , 'Flag' , 1); while(1) sprintf('in loop') Flag=getappdata(0,'Flag'); if status == 0 break; end drawnow end
You yourself said you have defined loop in the main program. Place the later part of code that i gave you in that program. Make changes as per your need like replacing while with for. And your pushbutton callback would only be:
setappdata(0 , 'Flag' , 0); % to be mentioned under stop pushbutton callback
What you did wrong was putting the whole code under same pushbutton, that sets the Flag to 1 before entering into the loop making it go into infinite loop.
Hello Dishant, I try the same as per your suggestion but still the GUI's STOP push_button is not working and my programs for loop iterations are continue as in previous case. I want to stop these iterations with my STOP push_button and later I will again resume iterations with my RUN push_button. Please let me know if you can help me in other way round. Your help is highly appreciated.
Regards, K.D.Singh
post your code. Attach you script files so that I can see what you did wrong.
Hello Dishant, Please find my Main program script format and its corresponding GUI's STOP-push_button code (suggested by you) as following,
Main Program Script:
for phi=1:0.1:10 for e=1:0.1:10 for h=1:0.1:10 for Bo=1:0.1:10 for b1=1:0.1:10 for c1=1:0.1:10 for b2=1:0.1:10 for c2=1:0.1:10
CALCULATION FORMULAS (to calculate value)
if value<value1 disp (count); % loop iterations
setappdata(0, 'Flag' , 1);
while(1) fprintf (2, 'looping...\n' ); Flag=getappdata(0,'Flag'); if Flag == 0 break; end drawnow end end end end end end end end end end
For STOP-push_button in GUI I have used your code like this:
% --- Executes on button press in pushbutton. function pushbutton_Callback(hObject, eventdata, handles) % hObject handle to pushbutton9 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) setappdata(0, 'Flag' , 0); % to be mentioned under stop pushbutton callback
I Hope it will work for you...thanks !!
To break out of nested loops check out for flag every time you enter inside a loop. Alter you main script like this:
setappdata(0 , 'Flag' , 1)
for ii = 1:10
if getappdata(0 ,'Flag') == 0
break
end
drawnow
for jj = 1:10
if getappdata(0 , 'Flag') == 0
break
end
drawnow
disp(jj)
end
end
And your stop pushbutton callback looks fine
Thanks Dishant, Now it is working correctly. Can you help in one more thing, if I want to re-run my same main script for further iterations using same RUN push_button, what would be the script?
Thanks for your time and kindness,
Best Regards, K.D.Singh
call that script file by name inside your run pushbutton callback.
Thanks Dishant, I want to make a display button in my matlab GUI who can automatically show the running FOR loop iterations/ counts just like it shows in matlab workspace during programming running. e.g. 1,2,3...........200,....400....1000.....n-iterations
How to make such type of GUI DISPLAY window and what could be the script of this DISPLAY callback function?
Thank you very much in advance!
-- K.D.Singh

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!