4

How to do threading in MATLAB? I want to run one function on two variables simultaneously. How do I do it?

1
  • 3
    @hai: please don't post duplicate questions. If you didn't get answers you liked for your previous question ([admin: now this question]), then you should edit it to make your problem clearer.
    – gnovice
    Apr 26, 2010 at 15:26

3 Answers 3

7

The parallel toolbox has some tools that might help you. Find below some example pasted from the Matlab help

matlabpool    % Use default parallel configuration
spmd          % By default uses all labs in the pool
    INP = load(['somedatafile' num2str(labindex) '.mat']);
    RES = somefun(INP);
end

Then the values of RES on the labs are accessible from the client as RES{1} from lab 1, RES{2} from lab 2, etc.

You might also look at parfor as a simple parallel replacement of for. Hope this helps even if it's not exactly what you're looking for.

1
  • In R2015a matlabpool function has been removed from Matlab, you need to call parpool instead.
    – hmfarimani
    Mar 6, 2017 at 13:43
1

I do not believe there is any built in multithreading support from MATLAB. This comes from both a conversation I had with a coworker recently and a quick google search

Hope this helps.

3
  • 1
    Yes. It is one of the problems with using Matlab to produce production apps.
    – mmr
    Apr 26, 2010 at 15:18
  • 1
    More and more of Matlab's native functions are multi-threaded with each new release to take advantage of multi-core CPUs. But the whole philosophy of tools like Matlab is against giving users hands-on access to threads and the like. Apr 26, 2010 at 15:47
  • 2
    From the Matlab help: There is either "implicit multiprocessing" which makes use of built in multi-threaded functions or "explicit multiprocessing" for which you have to use the parallel computing toolbox.
    – Adrien
    Apr 26, 2010 at 16:36
0

You may do it with MEX files and std::thread (see here).

I have not tried to call mexEvalString from the MEX file, and quite possibly it will result in a runtime error or freeze MATLAB for the time of execution. But if you can write that specific piece of code in C++, it may be what you're looking for.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.