"parfeval" and "backgroundPool" to create background progress bar

9 views (last 30 days)
Dear experts, I am trying to cretae a progress bar that runs in background.
My silly progressbar code is:
function [WaitF] = DBWaitGUI2
ScreSize = get(0,'screensize');
ScreSize = ScreSize(3:end);
matBar(:,:,1) = ones(256,256);
matBar(:,:,2) = zeros(256,256);
matBar(:,:,3) = zeros(256,256);
matBarg(:,:,1) = ones(256,256).*0.6;
matBarg(:,:,2) = ones(256,256).*0.6;
matBarg(:,:,3) = ones(256,256).*0.6;
WaitF = figure('Name','','NumberTitle','off','Color',[0.4 0.4 0.4],...
'Position',[ScreSize(1)/2-200 ScreSize(2)/2-50 400 60],...
'MenuBar','none');
% undecorateFig(WaitF)
sub1 = subplot('Position',[0.012 0.1 0.08 0.8],'Units','Normalized');
b1 = imagesc(sub1,matBar);
axis off
box off
AChilds.b1 = b1;
sub2 = subplot('Position',[0.12-0.013 0.1 0.08 0.8],'Units','Normalized');
b2 = imagesc(sub2,matBarg);
axis off
box off
AChilds.b2 = b2;
sub3 = subplot('Position',[0.22-0.013 0.1 0.08 0.8],'Units','Normalized');
b3 = imagesc(sub3,matBarg);
axis off
box off
AChilds.b3 = b3;
sub4 = subplot('Position',[0.32-0.013 0.1 0.08 0.8],'Units','Normalized');
b4 = imagesc(sub4,matBarg);
axis off
box off
AChilds.b4 = b4;
sub5 = subplot('Position',[0.42-0.013 0.1 0.08 0.8],'Units','Normalized');
b5 = imagesc(sub5,matBarg);
axis off
box off
AChilds.b5 = b5;
sub6 = subplot('Position',[0.52-0.013 0.1 0.08 0.8],'Units','Normalized');
b6 = imagesc(sub6,matBarg);
axis off
box off
AChilds.b6 = b6;
sub7 = subplot('Position',[0.62-0.013 0.1 0.08 0.8],'Units','Normalized');
b7 = imagesc(sub7,matBarg);
axis off
box off
AChilds.b7 = b7;
sub8 = subplot('Position',[0.72-0.013 0.1 0.08 0.8],'Units','Normalized');
b8 = imagesc(sub8,matBarg);
axis off
box off
AChilds.b8 = b8;
sub9 = subplot('Position',[0.82-0.013 0.1 0.08 0.8],'Units','Normalized');
b9 = imagesc(sub9,matBarg);
axis off
box off
AChilds.b9 = b9;
sub10 = subplot('Position',[0.92-0.013 0.1 0.08 0.8],'Units','Normalized');
b10 = imagesc(sub10,matBarg);
axis off
box off
AChilds.b10 = b10;
kee=0;
while kee == 0
for iu = 1:10
if iu>1
eval(['AChilds.b',num2str(iu-1),'.CData = matBarg;']);
else
eval(['AChilds.b',num2str(10),'.CData = matBarg;']);
end
eval(['AChilds.b',num2str(iu),'.CData = matBar;']);
pause(0.1);
end
end
end
So I am trying:
parfeval(backgroundPool,DBWaitGUI2);
The progress bar looks fine, but, Matlab is still waiting for the infinite process to end. Any idea of how to solve it? Am I committing any sin?

Accepted Answer

Raymond Norris
Raymond Norris on 19 Oct 2021
Edited: Raymond Norris on 19 Oct 2021
There are a couple of issues
  • parfeval requires other input arguments and you need to pass in the function handle to DBWaitGUI2. At the moment, you're calling DBWaitGUI2 directly, rather then delaying it on the background thread.
f = parfeval(backgroundPool,@DBWaitGUI2,1);
This states there's 1 output argument (WaitF), though you don't need to return it (you could give parfeval an input argument of 0). If there were any input arguments to DBWaitGUI2, you provide those after the number of input arguments.
  • But there's a bigger issue here. You need to run the plotting on the main thread, not a threads-based pool or a background thread. Alternatively, you can use a process-based pool. To see this, run the following:
f.fetchOutput
It will complain about get, but this is easy to reproduce with just
f = parfeval(backgroundPool,@plot,1,1:10);
A good example of calculation vs plotting with parfeval is in the documentation
Look at how the waitbar is updated (implicilty on the main thread). With that said, going back to the alternative (using a process-based pool), the plotting will run on a headless worker -- it won't display the GUI on the main thread, as seen here
local = parcluster('local');
pool = local.parpool;
f = pool.parfeval(@plot,1,1:10);

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!