- https://uk.mathworks.com/help/matlab/ref/parallel.future.aftereach.html
- https://uk.mathworks.com/help/matlab/ref/parallel.future.afterall.html
Integrating Parallel Computing with App Designer for Real-Time UI Updates
36 views (last 30 days)
Show older comments
I’ve developed an application with three panels, each responsible for a different task, and I need to update the UI in real-time. However, I’m running into issues because App Designer doesn’t support threading directly and struggles with accessing app property variables. I would like to utilize the Parallel Computing Toolbox to enable parallel execution of tasks and update the UI simultaneously.
I’ve already tried using parfor, parfeval, and spmd to handle parallel execution, but I’m facing difficulties with updating the app property variables in real-time within the App Designer environment. Specifically, these approaches are not integrating well with the app's UI and property variables.
Can you provide guidance on how to properly integrate parallel computing with App Designer while ensuring the app property variables are updated correctly in real-time? Any tips or solutions to resolve this issue would be very helpful!
0 Comments
Answers (2)
Rick Amos
on 7 Nov 2024 at 16:02
My recommendation is to use a mix of parfeval (to launch parallel work) with afterEach/afterAll (to update the app in response to that work finishing):
You are allowed to update the App inside the afterEach/afterAll function. This means you can think of these as like Callbacks to parallel work.
For an example, create an AppDesigner app named "MyApp" with a button and a UIAxis. Then add the following code:
classdef MyApp < .. % AppDesigner app with a "Start" button and a single UI Axes
methods (Static)
function result = doSomeWork()
pause(1);
result = rand(100, 1);
end
end
methods
% One of the doSomeWork tasks finished with a result
function OneTaskFinished(app, result)
hold(app.UIAxes, "on");
plot(app.UIAxes, result);
hold(app.UIAxes, "off");
drawnow("limitrate", "nocallback");
end
% All tasks finished. This receives the futures because
% PassFuture=true in the above afterAll
function AllTasksFinished(app, futures)
% Report the first error if one is present
for ii = 1:numel(futures)
if ~isempty(futures(ii).Error)
errordlg(getReport(future(ii).Error));
end
end
%... Make the App look like it's idle again
app.Button.Enable = true;
end
end
methods
% Callback for a Start button (add callback through AppDesigner)
function ButtonPushed(app, event)
%... Make the App look like it's running
app.Button.Enable = false;
% Launch some parallel work
for ii = 1:10
futures(ii) = parfeval(@MyApp.doSomeWork, 1);
end
% Setup an app callback run per task
afterEach(futures, @app.OneTaskFinished, 0);
% Setup an app callback run once all work finishes
% PassFuture allows us to understand if any task errored.
afterAll(futures, @app.AllTasksFinished, 0, PassFuture=true);
end
end
end
3 Comments
Walter Roberson
on 11 Nov 2024 at 17:21
Currently the call is
futures(ii) = parfeval(@MyApp.doSomeWork, 1);
You can add additional parameters after the 1 such as
futures(ii) = parfeval(@MyApp.doSomeWork, 1, MyApp.SomeEditField.Value);
You would need to alter the function signature
function result = doSomeWork()
to expect the parameter.
You can alter function OneTaskFinished to set an edit field. However, function OneTaskFinished will be invoked several times (once for each "future" created) and it is questionable which edit field you should set. I guess you could just append each result to the end of an edit field.
BHARATH
about 4 hours ago
Edited: BHARATH
2 minutes ago
Thanks for your answer earlier, but I got struck at MATLAB App Designer with four tabs, each handling a distinct process:
Tab 1: Processes data from a Analog Input and Output interface from DAQ.
Tab 2: Manages communication over a CAN protocol.
Tab 3: Handles data imports and processing data from excel sheet.
Tab 4: Provides a UI for controlling and monitoring the other tabs.
In Tab 4, a button triggers the processes in Tabs 1, 2, and 3 to start one after the other. However, only the first two processes are triggering successfully, while the third one doesn’t start. Each process runs continuously, and I suspect the issue may be due to blocking or conflicts between the running processes.
Questions:
- How can I ensure all processes trigger sequentially without one blocking the other?
- What is the best way to handle data synchronization across continuously running processes to prevent conflicts?
Any guidance or examples for managing synchronization across multiple tabs would be great.
Thank you!
Walter Roberson
on 7 Nov 2024 at 19:00
The only Mathworks product that is designed for Real-Time work is Simulink Real-Time.
You will never be able to get Real-Time performance from MATLAB (including App Designer.) MATLAB is not designed for Real-Time and will never be designed for Real-Time.
0 Comments
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!