How to opening 1 instance of MATLAB from MATLAB, but accessing it multiple times
57 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I would like to do the followings:
- From a MATLAB session, open another instance of MATLAB - which can be done by: !matlab &
- Every now and then from existing MATLAB session have a command executed in the other session. This is the part that I do not know how to do. How in existing MATLAB session, I can have a command exscuted on another instance I already opened.
I greatly appreciate your suggestions.
3 件のコメント
Raymond Norris
2021 年 8 月 17 日
Does the second MATLAB have to have its Desktop visible to interact with or does it suffice to be running in "headless" mode?
Do you have the Parallel Computing Toolbox?
採用された回答
Raymond Norris
2021 年 8 月 18 日
Using the Parallel Computing Toolbox, look at parpool and parfeval. parpool will start a N number of "workers" (i.e. headless instances of MATLAB), which can be running either on your local machine (supported out of the box) or on a remote machine (additional work to setup). parfeval offloads execution to a worker. For example
% Initialization: get a handle to the cluster and start a pool of 1 worker
cluster = parcluster;
pool = cluster.parpool(1);
% ... run other code ...
% Spawn command to run on worker
pool.parfeval(@mycode,0,{});
parfeval is non-blocking and can be called whenever you want to offload code. The {} is the input arguements to calling mycode. If you code has input args, assign them into {}.
If your code returns a result, then you'll need to assign it a result so that you can pull the result(s) back. You won't be notified when the code finishes, so you'll need to verify that the state has finished.
f = pool.parfeval(@mycode,1,{});
% ... run other code ...
% Get results
f.wait
result = f.fetchOutputs;
See the doc for parfeval for more information. One consideration is that a pool will expire if not used after a period of time (default is 30 minutes). You can disable this auto-deletion in the parallel preferences.
6 件のコメント
Raymond Norris
2021 年 8 月 19 日
Correct, use the cancel() method to end the task (not interrupt). For example
>> local = parcluster('local');
>> pool = local.parpool(1);
Starting parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 1).
>>
>> % Run task for 10 hours
>> f = pool.parfeval(@(t)pause(t),0,60*60*10);
>> f.State
ans =
'running'
>> % Cancel task preemptively
>> f.cancel
>> f.State
ans =
'finished'
>>
その他の回答 (2 件)
Walter Roberson
2021 年 8 月 18 日
As you are using Windows, one approach is to use .NET System.Diagnostics.Process https://www.mathworks.com/matlabcentral/answers/586706-how-to-redirect-standard-input-and-standard-output-using-net-system-diagnostics-process#answer_487475 to open a process connection, and write commands to the other MATLAB. The link shows how to redirect input and output.
Another possibility is to invoke MATLAB through actxserver() and use the Automation Engine facilities; https://www.mathworks.com/help/matlab/call-matlab-com-automation-server.html
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Parallel Computing Fundamentals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!