running parallel loop until a variable or event

1 visualización (últimos 30 días)
Juan Jose Ortiz Torres
Juan Jose Ortiz Torres el 19 de Abr. de 2019
Respondida: Ronit el 21 de Ag. de 2024
Hi guys, I'm trying to make a button blink every time just when a varriable or event change:
function blink(hObject, eventdata, parent_GUI)
handles = guidata(parent_GUI);
parfor i=1:3
if get(handles.boton_circulo_visible_in,'Value')==0
i=i-1;
end
set(handles.CONECTAR,'ForegroundColor',[1,1,1]);
disp('prendido')
pause(0.5)
set(handles.CONECTAR,'ForegroundColor',[0,0,0]);
disp('apagado')
pause(0.5)
end
I tried with parfor loop but it say show me an error " changing the loop index is invalid inside a parfor loop iteration". So i loop up abaout other parallel loop like spmd, but also it executes only single time. I dont know what can i do to make that iterative until a variable change
Thanks

Respuestas (1)

Ronit
Ronit el 21 de Ag. de 2024
Hello Juan,
The error you're encountering is since parfor does not allow modification of the loop index within the loop body. Additionally, parfor is intended for parallel execution, which might not be necessary for your blinking button functionality. Instead, you can use a while loop to continuously check the variable's value and blink the button accordingly.
function blink(hObject, eventdata, parent_GUI)
handles = guidata(parent_GUI);
while true
% Check the condition to continue blinking
if get(handles.boton_circulo_visible_in, 'Value') == 0
% Button is off, keep blinking
set(handles.CONECTAR, 'ForegroundColor', [1, 1, 1]);
disp('prendido');
pause(0.5);
set(handles.CONECTAR, 'ForegroundColor', [0, 0, 0]);
disp('apagado');
pause(0.5);
else
% Button is on, stop blinking
break;
end
end
end
I hope it helps your query!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by