How to run .m files in one thread from an app running in a different thread so that I can interact with the app during .m file execution?

5 views (last 30 days)
I have an app that I have made programmatically (without appdesigner) that frequently calls already existing functions located in .m files when I click certain buttons. Currently, the app itself and the functions it calls are all run in the foreground. However, these functions can take a long time to execute (several hours), during which time I cannot interact with the app (because callbacks won't execute until after the functions finish running).
The documentation on backgroundPool and parfeval is a bit confusing to me. Is there a way to make the app run on one pool, and the functions in the .m files to run on another thread? I have access to the Parallel Computing Toolboox. Or, would it require running two instances of MATLAB simultaneously? Is it possible to share data across two instances of MATLAB?

Answers (1)

Raymond Norris
Raymond Norris on 7 Sep 2022
@Mitchell Tillman you might consider refactoring the code so that the callbacks call parfeval with backgroundPool, but keep in the app running on MATLAB's main execution thread. For instance instead of
function PlotCallback(..)
data = ..
function PlotCallback(...)
% Spawn a thread to handle the plotting
parfeval(backgroundPool,@plotdata, ..)
% Come back immediately to free up the App
I'm leaving an awful lot out here, but hopefully you get the idea
Walter Roberson
Walter Roberson on 9 Sep 2022
When you have parallel pool, or background thread pool, then the only way the workers can display graphics is to send data to the client for the client to display, or send graphics objects to the client for the client to display.
When you have separate MATLAB instances, then the above methods are still possible (if you use one of several ways to send data between processes.) You also gain the possibility that the second instance is running on the same display and so can display graphics directly in its own area (but not cannot control graphics for the first MATLAB instance.) Running on the same display typically involves the second MATLAB running on the same host, but if you are using Linux with X-Windows then in theory they could be remote; likewise you could in theory be running a remote desktop type of application to display graphics created on a different host.
There are no mechanisms provided to tightly couple multiple MATLAB instances -- at least none other than parfor / spmd / parfeval / batch

Sign in to comment.


Find more on Startup and Shutdown 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!