How to stop a "while" iteration loop inside a callback push button subroutine, clicking another push button?

I wrote a "gui" to take temperature data from a sensor connected to Arduino board, and plot it. All of that, using Arduino package support. The action start when i click "Register" push button, and it goes right, cause I give it a specific time to plot.
But, I want to extend plotting duration indefinitely, until I click a "Stop" push button. This is the code inside "registerButton" callback:
delete(instrfind('Type', 'serial'));
puerto = get(handles.portEdit, 'String');
delete(instrfind({'Port'},{puerto}));
a = arduino(puerto);
for i = 2:19
a.pinMode(i, 'input')
end
handles.a = a;
t_s = str2double(get(handles.tiempoButton, 'String'));
% Used only when i need a specific time
a_pin = str2double(get(handles.pinAnalogButton, 'String'));
escala = str2double(get(handles.scaleFactorEdit, 'String'));
Y(1) = handles.a.analogRead(a_pin)*escala;
t(1) = 0;
tic;
handles.q = false;
i = 2;
while(handles.q == false)
Y(i) = handles.a.analogRead(a_pin)*escala;
t(i) = toc;
i = i + 1;
plot(t,Y), grid on;
pause(0.01)
end
Here, "handles.q" represents the state of "stopButton". "false" means "stopButton" isn't pressed and "while" loop goes on, until I click it, so, loop ends. I would like to replace "handles.q == false" with the right code.
How can I do that?

 Respuesta aceptada

You might be able to assign handles.q = false in your other callback while this one is running. Just be sure to call guidata() so that handles is updated so that other functions can see that you've changed the value of handles.q.

3 comentarios

Oscar
Oscar el 22 de Sept. de 2012
Editada: Oscar el 22 de Sept. de 2012
Actually, That's what I did. The code in the "stopButton" callback subroutine is:
q = true;
handles.q = q;
handles.output = hObject;
guidata(hObject, handles);
Just like this, the gui runs and plots the data since i push "regisButton". Then, I push "stopButton", but nothing happens, plot continues and the programs falls down into an infinite loop, so, I have to close the gui window to collapse the program ("guidata(hObject, handles);" is at the end of callback "regisButton" subroutine, too) Where is the mistake?
However, i'm gonna see the links you suggested, of course.
Perhaps you need to call guidata inside the loop to pull the updated handles structure into that function.
First of all, thanks for your help and your valuable time. it worked. These are the code lines i added a the end inside "while" loop:
data = guidata(handles.stopButton);
handles.q = data.q;

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 21 de Sept. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by