simultaneous loops in GUI (updating text box)

1 view (last 30 days)
Arved
Arved on 25 Nov 2015
Answered: Arved on 28 Nov 2015
Hello Guys.
I got a problem with my GUI. I created three Buttons "Start1", "Start2" and "Stop" and two static text boxes "text1" and "text2". Pressing the Start buttons will start two loops in the associated callback functions, which represent an infinite sum (untill i click the "Stop" button) and will set the 'string' property of the text boxes to the actual sum value. The callback functions are looking like that (the global variable stop is initialized in the Openingfc):
% --- Executes on button press in start1_pushbutton.
function start1_pushbutton_Callback(hObject, eventdata, handles)
global stop;
a = 0;
while stop
a = a +1;
set(handles.text1,'string',num2str(a));
drawnow;
end
stop = 1;
% --- Executes on button press in start2_pushbutton.
function start2_pushbutton_Callback(hObject, eventdata, handles)
global stop;
a = 0;
while stop
a = a +1;
set(handles.text2,'string',num2str(a));
drawnow;
end
stop = 1;
% --- Executes on button press in stop_pushbutton.
function stop_pushbutton_Callback(hObject, eventdata, handles)
global stop;
stop = 0;
The problem is, that only one text box is getting updated, when i click on both start buttons. It seems, that the loops dont execute simultaneous. When i click the "Stop" button, only one loop is breaking and the other one is still running. But i need to update both text boxes simultaneous. Is there a solution for this? The Background is, that I'm writing a GUI to control two stepper motors. I need to see the actual position of both motors (x-axis y-axis) simultaneous (= updating the "string" property of two text boxes using two while loops).
Thanks for your help.
PS: Sorry for my bad english.

Answers (2)

Adam
Adam on 25 Nov 2015
Edited: Adam on 25 Nov 2015
Code in callbacks (and anywhere else outwith using the parallel toolbox) will always run sequentially. How it does that depends on settings - e.g.
UI components have an 'Interruptible' property which can be set to 'on' or 'off' and this will determine whether a second callback can interrupt the first or whether the second callback simply gets cancelled or queued. This latter decision is determined by the UI control's 'BusyAction' property which can be set to either 'queue' or 'cancel' (both self-explanatory). BusyAction is ignored if 'Interruptible' is set to 'on', in which case execution of the first callback is stopped at the next available point in the code.
All that information is not really relevant to you though because the underlying point is that code runs sequentially, all you can control with these settings is the behaviour when a second callback attempts to interrupt one already running.
Why do you need two start buttons? If you just updated both text boxes from inside a single loop callback you wouldn't have this problem at all.

Arved
Arved on 28 Nov 2015
Thanks for your reply! :) I should rethink my method to update both text boxes.

Categories

Find more on Environment and Settings 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!