How to do setup data queue handling between app designer and parallel thread?
Hello community
I want to use the Parallel Pool Box in App Designer to run a parallel thread and pass information between the two entitiies.
Imagine the following use case: You want to start, pause and abort i.e. a sequence of measurements from the GUI
In case the start is activated the start button changes to pause button and starts in parallel the measurement sequence.
In case the executions is paused, the current step of the sequence is allowed to be finished.
In case of abort the measurement shall be stopped immediatly and the instruments gracefully turned off.
I tried to represent this in a workflow (shown at end of post- file follows as soon as upload works again). And did setup a simplified .mlapp first trying to start a parallel thread queueing information and the main code fetching that information and diplaying it in a text area. But in that form I cannot save it as .mlapp file. From what I’ve read so far in the forum (see list below) it seems to be an issue of handing over the app object to the parallel process. But I don’t understand what I am doing wrong and thus think I missed an important part of the data queue handling. I would be glad for any hint/information/support to fix this issue and learn from that mistake.
Further information I checked prior to set up .mlapp:
https://ch.mathworks.com/matlabcentral/answers/867683-how-to-use-threads-in-matlab-app-designer/
https://ch.mathworks.com/matlabcentral/answers/1413247-threading-in-matlab-gui-callbacks
https://ch.mathworks.com/matlabcentral/answers/2164600-integrating-parallel-computing-with-app-designer-for-real-time-ui-updates
– https://www.mathworks.com/matlabcentral/answers/366576-will-there-be-a-support-of-the-parallel-toolbox-in-matlab-app-designer#comment_720548
https://ch.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html
https://ch.mathworks.com/help/parallel-computing/parpool.html
https://ch.mathworks.com/help/parallel-computing/parallel.pool.pollabledataqueue.html
https://ch.mathworks.com/help/parallel-computing/receive-communications-on-workers.html
https://in.mathworks.com/matlabcentral/answers/1726505-proper-use-of-dataqueue-and-pollabledataqueue-inside-of-app-designer-class
Example code which can not be saved as .mlapp
classdef ExampleWorkflowAbortAsyn < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Abort2Button matlab.ui.control.StateButton
TextArea matlab.ui.control.TextArea
AbortButton matlab.ui.control.Button
StartButton matlab.ui.control.Button
end
properties (Access = public)
TCindex % Description
isPassed = false;
asycnWorker
dataQueue
clientQueue
helperClientQueue
end
methods (Static)
function receiveDataOnClient(data)
disp(data);
drawnow limitrate;
end
end
methods (Access = public)
function testExecutionMaster(app)
while strcmp(app.StartButton.Text, ‘Pause’)
app.isPassed = false;
updateUILog(app,sprintf(‘TC %i’, app.TCindex));
app.TCindex = app.TCindex + 1;
updateUILog(app,sprintf(‘tPrepare measurement…’));
pause(1)
updateUILog(app,sprintf(‘tExecute measurement…’));
pause(1)
updateUILog(app,sprintf(‘tAnalyze Measurement…’));
pause(1)
updateUILog(app,sprintf(‘t Test passed :)’));
app.isPassed = true;
end
end
function abortMeasurementAction(app)
% nice to have: pop up with abort error and instructions
updateUILog(app,’User triggerd abort starts’);
updateUILog(app,sprintf(‘tAll devices off’));
end
function updateUILog(app,message)
textArray = app.TextArea.Value;
textArray{end+1} = message;
app.TextArea.Value = textArray;
scroll(app.TextArea, ‘bottom’)
% createDiaryEntry(app, message)
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function EnterTestState(app, event)
switch app.StartButton.Text
case ‘Start’
app.StartButton.Text = ‘Pause’;
app.TCindex = 1;
asycnWorker = parpool(‘Threads’,1);
clientQueue = parallel.pool.DataQueue;
afterEach(clientQueue,@(data) app.receiveDataOnClient(data));
helperClientQueue = parallel.pool.PollableDataQueue;
temp = app.StartButton.Text;
wkrF = parallel.FevalFuture;
wkrF = parfeval(@testExecutionWorker,0,temp);
wkrQueue = poll(helperClientQueue,inf);
send(wkrQueue.Queue,"Start generating data");
case ‘Pause’
app.StartButton.Text = ‘Resume’;
updateUILog(app,’Test execution enters pause state’)
while ~app.isPassed
pause(0.2)
end
updateUILog(app,sprintf(‘Test execution paused at %i’, app.TCindex));
case ‘Resume’
app.StartButton.Text = ‘Pause’;
updateUILog(app,’Resume test execution’);
testExecutionMaster(app)
end
updateUILog(app,"End of EnterTestStateFunction")
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘MATLAB App’;
% Create StartButton
app.StartButton = uibutton(app.UIFigure, ‘push’);
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @EnterTestState, true);
app.StartButton.Position = [422 321 138 45];
app.StartButton.Text = ‘Start’;
% Create AbortButton
app.AbortButton = uibutton(app.UIFigure, ‘push’);
app.AbortButton.Position = [422 249 138 41];
app.AbortButton.Text = ‘Abort’;
% Create TextArea
app.TextArea = uitextarea(app.UIFigure);
app.TextArea.Editable = ‘off’;
app.TextArea.Position = [60 28 475 168];
% Create Abort2Button
app.Abort2Button = uibutton(app.UIFigure, ‘state’);
app.Abort2Button.Text = ‘Abort2’;
app.Abort2Button.Position = [129 289 186 54];
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = ExampleWorkflowAbortAsyn
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
endHello community
I want to use the Parallel Pool Box in App Designer to run a parallel thread and pass information between the two entitiies.
Imagine the following use case: You want to start, pause and abort i.e. a sequence of measurements from the GUI
In case the start is activated the start button changes to pause button and starts in parallel the measurement sequence.
In case the executions is paused, the current step of the sequence is allowed to be finished.
In case of abort the measurement shall be stopped immediatly and the instruments gracefully turned off.
I tried to represent this in a workflow (shown at end of post- file follows as soon as upload works again). And did setup a simplified .mlapp first trying to start a parallel thread queueing information and the main code fetching that information and diplaying it in a text area. But in that form I cannot save it as .mlapp file. From what I’ve read so far in the forum (see list below) it seems to be an issue of handing over the app object to the parallel process. But I don’t understand what I am doing wrong and thus think I missed an important part of the data queue handling. I would be glad for any hint/information/support to fix this issue and learn from that mistake.
Further information I checked prior to set up .mlapp:
https://ch.mathworks.com/matlabcentral/answers/867683-how-to-use-threads-in-matlab-app-designer/
https://ch.mathworks.com/matlabcentral/answers/1413247-threading-in-matlab-gui-callbacks
https://ch.mathworks.com/matlabcentral/answers/2164600-integrating-parallel-computing-with-app-designer-for-real-time-ui-updates
– https://www.mathworks.com/matlabcentral/answers/366576-will-there-be-a-support-of-the-parallel-toolbox-in-matlab-app-designer#comment_720548
https://ch.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html
https://ch.mathworks.com/help/parallel-computing/parpool.html
https://ch.mathworks.com/help/parallel-computing/parallel.pool.pollabledataqueue.html
https://ch.mathworks.com/help/parallel-computing/receive-communications-on-workers.html
https://in.mathworks.com/matlabcentral/answers/1726505-proper-use-of-dataqueue-and-pollabledataqueue-inside-of-app-designer-class
Example code which can not be saved as .mlapp
classdef ExampleWorkflowAbortAsyn < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Abort2Button matlab.ui.control.StateButton
TextArea matlab.ui.control.TextArea
AbortButton matlab.ui.control.Button
StartButton matlab.ui.control.Button
end
properties (Access = public)
TCindex % Description
isPassed = false;
asycnWorker
dataQueue
clientQueue
helperClientQueue
end
methods (Static)
function receiveDataOnClient(data)
disp(data);
drawnow limitrate;
end
end
methods (Access = public)
function testExecutionMaster(app)
while strcmp(app.StartButton.Text, ‘Pause’)
app.isPassed = false;
updateUILog(app,sprintf(‘TC %i’, app.TCindex));
app.TCindex = app.TCindex + 1;
updateUILog(app,sprintf(‘tPrepare measurement…’));
pause(1)
updateUILog(app,sprintf(‘tExecute measurement…’));
pause(1)
updateUILog(app,sprintf(‘tAnalyze Measurement…’));
pause(1)
updateUILog(app,sprintf(‘t Test passed :)’));
app.isPassed = true;
end
end
function abortMeasurementAction(app)
% nice to have: pop up with abort error and instructions
updateUILog(app,’User triggerd abort starts’);
updateUILog(app,sprintf(‘tAll devices off’));
end
function updateUILog(app,message)
textArray = app.TextArea.Value;
textArray{end+1} = message;
app.TextArea.Value = textArray;
scroll(app.TextArea, ‘bottom’)
% createDiaryEntry(app, message)
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function EnterTestState(app, event)
switch app.StartButton.Text
case ‘Start’
app.StartButton.Text = ‘Pause’;
app.TCindex = 1;
asycnWorker = parpool(‘Threads’,1);
clientQueue = parallel.pool.DataQueue;
afterEach(clientQueue,@(data) app.receiveDataOnClient(data));
helperClientQueue = parallel.pool.PollableDataQueue;
temp = app.StartButton.Text;
wkrF = parallel.FevalFuture;
wkrF = parfeval(@testExecutionWorker,0,temp);
wkrQueue = poll(helperClientQueue,inf);
send(wkrQueue.Queue,"Start generating data");
case ‘Pause’
app.StartButton.Text = ‘Resume’;
updateUILog(app,’Test execution enters pause state’)
while ~app.isPassed
pause(0.2)
end
updateUILog(app,sprintf(‘Test execution paused at %i’, app.TCindex));
case ‘Resume’
app.StartButton.Text = ‘Pause’;
updateUILog(app,’Resume test execution’);
testExecutionMaster(app)
end
updateUILog(app,"End of EnterTestStateFunction")
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘MATLAB App’;
% Create StartButton
app.StartButton = uibutton(app.UIFigure, ‘push’);
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @EnterTestState, true);
app.StartButton.Position = [422 321 138 45];
app.StartButton.Text = ‘Start’;
% Create AbortButton
app.AbortButton = uibutton(app.UIFigure, ‘push’);
app.AbortButton.Position = [422 249 138 41];
app.AbortButton.Text = ‘Abort’;
% Create TextArea
app.TextArea = uitextarea(app.UIFigure);
app.TextArea.Editable = ‘off’;
app.TextArea.Position = [60 28 475 168];
% Create Abort2Button
app.Abort2Button = uibutton(app.UIFigure, ‘state’);
app.Abort2Button.Text = ‘Abort2’;
app.Abort2Button.Position = [129 289 186 54];
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = ExampleWorkflowAbortAsyn
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end Hello community
I want to use the Parallel Pool Box in App Designer to run a parallel thread and pass information between the two entitiies.
Imagine the following use case: You want to start, pause and abort i.e. a sequence of measurements from the GUI
In case the start is activated the start button changes to pause button and starts in parallel the measurement sequence.
In case the executions is paused, the current step of the sequence is allowed to be finished.
In case of abort the measurement shall be stopped immediatly and the instruments gracefully turned off.
I tried to represent this in a workflow (shown at end of post- file follows as soon as upload works again). And did setup a simplified .mlapp first trying to start a parallel thread queueing information and the main code fetching that information and diplaying it in a text area. But in that form I cannot save it as .mlapp file. From what I’ve read so far in the forum (see list below) it seems to be an issue of handing over the app object to the parallel process. But I don’t understand what I am doing wrong and thus think I missed an important part of the data queue handling. I would be glad for any hint/information/support to fix this issue and learn from that mistake.
Further information I checked prior to set up .mlapp:
https://ch.mathworks.com/matlabcentral/answers/867683-how-to-use-threads-in-matlab-app-designer/
https://ch.mathworks.com/matlabcentral/answers/1413247-threading-in-matlab-gui-callbacks
https://ch.mathworks.com/matlabcentral/answers/2164600-integrating-parallel-computing-with-app-designer-for-real-time-ui-updates
– https://www.mathworks.com/matlabcentral/answers/366576-will-there-be-a-support-of-the-parallel-toolbox-in-matlab-app-designer#comment_720548
https://ch.mathworks.com/help/parallel-computing/parallel.pool.dataqueue.html
https://ch.mathworks.com/help/parallel-computing/parpool.html
https://ch.mathworks.com/help/parallel-computing/parallel.pool.pollabledataqueue.html
https://ch.mathworks.com/help/parallel-computing/receive-communications-on-workers.html
https://in.mathworks.com/matlabcentral/answers/1726505-proper-use-of-dataqueue-and-pollabledataqueue-inside-of-app-designer-class
Example code which can not be saved as .mlapp
classdef ExampleWorkflowAbortAsyn < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Abort2Button matlab.ui.control.StateButton
TextArea matlab.ui.control.TextArea
AbortButton matlab.ui.control.Button
StartButton matlab.ui.control.Button
end
properties (Access = public)
TCindex % Description
isPassed = false;
asycnWorker
dataQueue
clientQueue
helperClientQueue
end
methods (Static)
function receiveDataOnClient(data)
disp(data);
drawnow limitrate;
end
end
methods (Access = public)
function testExecutionMaster(app)
while strcmp(app.StartButton.Text, ‘Pause’)
app.isPassed = false;
updateUILog(app,sprintf(‘TC %i’, app.TCindex));
app.TCindex = app.TCindex + 1;
updateUILog(app,sprintf(‘tPrepare measurement…’));
pause(1)
updateUILog(app,sprintf(‘tExecute measurement…’));
pause(1)
updateUILog(app,sprintf(‘tAnalyze Measurement…’));
pause(1)
updateUILog(app,sprintf(‘t Test passed :)’));
app.isPassed = true;
end
end
function abortMeasurementAction(app)
% nice to have: pop up with abort error and instructions
updateUILog(app,’User triggerd abort starts’);
updateUILog(app,sprintf(‘tAll devices off’));
end
function updateUILog(app,message)
textArray = app.TextArea.Value;
textArray{end+1} = message;
app.TextArea.Value = textArray;
scroll(app.TextArea, ‘bottom’)
% createDiaryEntry(app, message)
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function EnterTestState(app, event)
switch app.StartButton.Text
case ‘Start’
app.StartButton.Text = ‘Pause’;
app.TCindex = 1;
asycnWorker = parpool(‘Threads’,1);
clientQueue = parallel.pool.DataQueue;
afterEach(clientQueue,@(data) app.receiveDataOnClient(data));
helperClientQueue = parallel.pool.PollableDataQueue;
temp = app.StartButton.Text;
wkrF = parallel.FevalFuture;
wkrF = parfeval(@testExecutionWorker,0,temp);
wkrQueue = poll(helperClientQueue,inf);
send(wkrQueue.Queue,"Start generating data");
case ‘Pause’
app.StartButton.Text = ‘Resume’;
updateUILog(app,’Test execution enters pause state’)
while ~app.isPassed
pause(0.2)
end
updateUILog(app,sprintf(‘Test execution paused at %i’, app.TCindex));
case ‘Resume’
app.StartButton.Text = ‘Pause’;
updateUILog(app,’Resume test execution’);
testExecutionMaster(app)
end
updateUILog(app,"End of EnterTestStateFunction")
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure(‘Visible’, ‘off’);
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = ‘MATLAB App’;
% Create StartButton
app.StartButton = uibutton(app.UIFigure, ‘push’);
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @EnterTestState, true);
app.StartButton.Position = [422 321 138 45];
app.StartButton.Text = ‘Start’;
% Create AbortButton
app.AbortButton = uibutton(app.UIFigure, ‘push’);
app.AbortButton.Position = [422 249 138 41];
app.AbortButton.Text = ‘Abort’;
% Create TextArea
app.TextArea = uitextarea(app.UIFigure);
app.TextArea.Editable = ‘off’;
app.TextArea.Position = [60 28 475 168];
% Create Abort2Button
app.Abort2Button = uibutton(app.UIFigure, ‘state’);
app.Abort2Button.Text = ‘Abort2’;
app.Abort2Button.Position = [129 289 186 54];
% Show the figure after all components are created
app.UIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = ExampleWorkflowAbortAsyn
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end appdesigner, parallel computing toolbox, dataqueue, asynchronous MATLAB Answers — New Questions