Integrating Parallel Computing with App Designer for Real-Time UI Updates

31 views (last 30 days)
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!

Answers (2)

Rick Amos
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
  2 Comments
BHARATH
BHARATH on 11 Nov 2024 at 11:55
Can we able to call an argument for function doSomeWork() and return that in a EditField.
Walter Roberson
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.

Sign in to comment.


Walter Roberson
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.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!