Running two MATLAB programs at the same time

Hi, I have a function in Matlab which depends on an input N. I need to run function(2),function(3),function(4),function(5) and perhaps also try other values of N. Each of these runs takes about two hours. I could run one after the other, but it would be much more efficient if I could just run them at the same time. Each run is independent of the other, so this should be doable, right?. I want to do the same one does when one runs a script in bash with nohup ./script.sh &, so you can send other scripts. How can I do this in Matlab?
Thanks

 Risposta accettata

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

6 Commenti

When I run code with
funcs = {@choose3DirRolling_Right, @choose3DirRolling_Left} ; % let fun1, fun2 be two functions
arguments = {updatedRightCoord,oxyzRightArrow;updatedUpCoord,oxyzUpArrow} ; % write the inputs of each function
solutions = cell(1,2); % initialize the solution
% use of parfor
parfor ii = 1:2
solutions{ii}=funcs{ii}(arguments{ii,:});
end
Error is Too many output arguments.
Can you helps please!
How many inputs does choose3DirRolling_Right have? How many inputs does choose3DirRolling_Left have?
choose3DirRolling_Right(updatedRightCoord,oxyzRightArrow);
choose3DirRolling_Left(updatedUpCoord,oxyzUpArrow);
There are two functions with two input vars as I put in arguments.
I created the simple program as below but it is the same error.
clc
x = 0:0.01:10;
y1 = sin(x);
y2 = cos(x);
funcs = {@func1, @func2} ; % let fun1, fun2 be two functions
arguments = {x y1;x y2} ; % write the inputs of each function
solutions = cell(1,2); % initialize the solution
% use of parfor
parfor ii = 1:2
solutions{ii}=funcs{ii}(arguments{ii,:});
end
with two functions:
function func1(x,y)
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
grid on, axis equal
end
%----------------
function func2(x,y)
plot(x, y), xlabel('x'), ylabel('Cos(x)'), title('Cos(x) Graph'),
grid on, axis equal
end
Your functions do not return any value but you try to assign their lack of value to solutions variable.
Thanks @Walter Roberson. Now I got the right answer. It should be return value for each function. But in the case of without the output value, how can I run it?

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 16 Ott 2017
You can use the Parallel Computing Toolbox, with batch() or parfeval()
Note: unless you configure specially, each worker would have access to one physical core. That can be a problem if your routines do a lot of mathematical calculations with large arrays: such calls are normally sent to an optimized library that automatically uses multiple threads. If this applies to your program, then running in parallel can end up taking longer than running individually.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by