Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

parallel processing - Run a script that uses multiple MATLAB sessions

How to call a function with simple inputs in several MATLAB sessions automatically?

The manual way to do it would be:

  • Open three sessions
  • Call magic(t) where t is 1, 2 or 3 respectively

So, my question is: How can I do this all programatically?

In case it is relevant, I do not want to use the parallel processing toolbox.


Note that I don't think a parfor loop can do what I want. First of all that would require the parallel processing toolbox, and secondly I want to be able to debug as soon as one of these operations fails, without bothering the other sessions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all a way must be found to open sessions programatically. Based on this and this it is found you can do it as follows (works on windows as well):

% Opening 3 matlab sessions
for t = 1:3
!matlab &
end

Besides simply opening them, a simple command can also be given

!matlab -r "magic(5)" &

Now, to finally combine this just a small trick remains:

for t = 1:3
   str = ['!matlab -r "magic(' num2str(t) ')" &'];
   eval(str)
end

Note that if you want to use more complicated inputs you can simply save them in a struct and call them with this index by using a wrapper script as called function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...