2

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.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122

2 Answers2

2

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.

Community
  • 1
  • 1
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • 1
    Note that an individual license of MATLAB allows you to open at most two simultaneous instances of MATLAB under that license. So unless you have access to a concurrent license (in which case each of the instances started above would check out a license, costing much more than a copy of Parallel Computing Toolbox), the above would be in violation of the license agreement. – Sam Roberts Aug 13 '13 at 09:13
  • @SamRoberts Though I didn't think about this at first, upon reading our license agreement I don't seem to violate it. Also though I don't haven't found the license for individuals, I recall it limiting the amount of users/computers. Not sure about sessions though. – Dennis Jaheruddin Aug 13 '13 at 09:38
  • 3
    Actually, on rereading the licence I think perhaps you're right. It does seem to restrict only to a maximum of two *computers* simultaneously, rather than two instances of MATLAB simultaneously as I had thought. I retract my comment - although readers should still be aware that they should consider any potential licensing implications of what you suggest doing, and that neither of us are lawyers (unless you are :) – Sam Roberts Aug 13 '13 at 10:02
1

You could try Multicore, which uses several instances of Matlab to do what parfor does by passing information via a common directory. If you can rewrite your code loop to call a function that returns values then Multicore might do what you are looking for.

http://www.mathworks.com/matlabcentral/fileexchange/13775-multicore-parallel-processing-on-multiple-cores

Steve C
  • 56
  • 1