Update app.textarea inside a parfor loop

3 visualizaciones (últimos 30 días)
Nick LaSorte
Nick LaSorte el 7 de Mzo. de 2018
Comentada: Walter Roberson el 23 de Ag. de 2020
I'm trying to update app.TextArea in the app designer using parfor_progress
Error: Valid indices for 'app' are restricted in PARFOR loops.
How would I fix the sliced variable that is 'app' or is it just impossible with parfor and the app designer?
Using Matlab 2017b
Example code below:
clear;
clc;
num_iter=100;
percent=parfor_progress(num_iter);
parfor i=1:num_iter
pause(randi(10));
percent=parfor_progress;
app.TextArea.Value={percent};
end
parfor_progress(0);
Any help/links for further reading would be helpful.
Thanks
Nick

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Mzo. de 2018
If you are using a newer release then I would skip all of that and use parallel.pool.DataQueue and send() and afterEach(); see https://www.mathworks.com/help/distcomp/send.html .

Más respuestas (1)

Ashadullah Shawon
Ashadullah Shawon el 19 de Jul. de 2019
i am just expanding the accepted answer for the quick understanding. I have uploaded the full appdesigner code and here is also the functions preview using parallel.pool.DataQueue and send() and afterEach()
methods (Access = private)
function app= func1(app,data)
%disp(data);
app.textTextArea.Value = strcat('Function 1----',datestr(now));
%pause(1);
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 1----',datestr(now))];
end
function app= func2(app,data)
%disp('Function 2');
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 2----',datestr(now))];
%pause(1);
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 2----',datestr(now))];
end
end
methods (Access = private)
% Value changed function: ClickOnButton
function ClickOnButtonValueChanged(app, event)
q = parallel.pool.DataQueue;
r = parallel.pool.DataQueue;
afterEach(q, @app.func1);
afterEach(r, @app.func2);
parfor i = 1:2
if i == 1
%func1(app);
send(q,i);
else
%func2(app);
send(r,i);
end
end
end
end
  3 comentarios
Alexander Babin
Alexander Babin el 23 de Ag. de 2020
function sendMessage(app,data)
app.ProgressTextArea.Value=[app.ProgressTextArea.Value;data];
end
message = parallel.pool.DataQueue;
afterEach(message, @(msg) sendMessage(app,msg));
parfor iteration=1:N
...
msg = ['Completed iteration # ', num2str(iteration)];
send(message,msg);
end
Walter Roberson
Walter Roberson el 23 de Ag. de 2020
you might need to add a drawnow call

Iniciar sesión para comentar.

Categorías

Más información sobre Parallel for-Loops (parfor) 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