how to run two matlab scripts in parallel?

I need to run two scripts in parallel and simultaneously, run the first script and the second script at the same time.

1 件のコメント

Hema sai sree
Hema sai sree 2025 年 2 月 26 日
編集済み: Walter Roberson 2025 年 2 月 26 日
funcs1 = {@fun1, @fun2} ; % let fun1, fun2 be two functions
arguments = {inp1,inp2 ;inp1,inp2} ; % write the inputs of each function
solutions = cell(1,2); % initialize the solution
% use of parfor
parfor ii = 1:2
solutions1{ii}=funcs1{ii}(arguments1{ii,:});
end
M = solutions1{1} ; N = solutions1{2} ; % assign the results

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2017 年 1 月 2 日

5 投票

You can use parfor to run two functions simultaneously. Eg:
funcs1 = {@fun1, @fun2} ; % let fun1, fun2 be two functions
arguments = {inp1,inp2 ;inp1,inp2} ; % write the inputs of each function
solutions = cell(1,2); % initialize the solution
% use of parfor
parfor ii = 1:2
solutions1{ii}=funcs1{ii}(arguments1{ii,:});
end
M = solutions1{1} ; N = solutions1{2} ; % assign the results

その他の回答 (4 件)

José-Luis
José-Luis 2017 年 1 月 2 日
編集済み: José-Luis 2017 年 1 月 2 日

0 投票

You could use parpool(). More flexible than parfor() but you'd need the parallel computing toolbox.
Another alternative is to start two sessions of Matlab and run the different scripts there.

4 件のコメント

Walter Roberson
Walter Roberson 2019 年 1 月 5 日
parpool initializes conditions for running simultaneous workers, but does not in itself send any computation to the workers. parfor() and spmd() and parfeval() are some of the methods of submitting tasks to a parallel pool created with parpool.
Hamza Ashraf
Hamza Ashraf 2020 年 9 月 25 日
hi am working on a project that requires running two tasks simultaneosuly.
task 1. %infinite while loop which constantly records audio data and detects for ambulance and stores result in a variable
task 2. % again infinite loop which is processing some images and gives output to arduino and switch traffic signals according to results
task 1 is independent. But task 2 requires data form task 1 so that it can gives output to arduino if an ambulance is detected and perform assigned action according to ambulance detection
can you help me how to do it
Walter Roberson
Walter Roberson 2020 年 9 月 25 日
SPMD with labSend() and labReceive() is natural for that kind of work.
Hamza Ashraf
Hamza Ashraf 2020 年 9 月 26 日
Can you explain it with code i dont know how to use spmd with labsend and lab receive

サインインしてコメントする。

Austin Bond
Austin Bond 2018 年 4 月 17 日

0 投票

should the 'arguments' and 'solutions' variables really be 'arguments1' and 'solutions1'?

1 件のコメント

Walter Roberson
Walter Roberson 2018 年 4 月 17 日
Yes, KSSV made some typing mistakes.

サインインしてコメントする。

Sean de Wolski
Sean de Wolski 2018 年 4 月 17 日

0 投票

Just call batch directly or right-click on the script in the current folder browser and select "Run as batch job"

MathWorks Support Team
MathWorks Support Team 2022 年 9 月 2 日

0 投票

You can use Parallel Computing Toolbox to run two MATLAB functions simultaneously. (If you have scripts, then first wrap them up as functions as described here in the doc Create Functions in Files). The parfeval function allows you to run two functions simultaneously on a parallel pool. Each call to parfeval causes the specified function to be invoked on a worker with the provided inputs. You also need to specify up front how many outputs you want. Your code might end up like this:
numOut = 1; % Both functions here have only a single output
f1 = parfeval(@myFirstFunction, numOut, in1, in2); % request myFirstFunction(in1,in2)
f2 = parfeval(@mySecondFunction, numOut, in3); % request mySecondFunction(in3)
out1 = fetchOutputs(f1); % this waits for myFirstFunction to complete and then gets the result
out2 = fetchOutputs(f2);
If you need to process the results as soon as either function completes, you could use fetchNext instead of fetchOutputs.

1 件のコメント

Walter Roberson
Walter Roberson 2022 年 9 月 2 日
These days, you can also use parfeval() and background threads without needing the Parallel Computing Toolbox; see backgroundPool

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeParallel Computing Toolbox についてさらに検索

質問済み:

2017 年 1 月 2 日

編集済み:

2025 年 2 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by