MATLAB: Running Matlab functions simultaneously

MATLABmatlab functionparallel computingParallel Computing Toolbox

I wonder if anyone could help me with this problem: I have two functions and the way they work is that they get some data and then they compute some matrices, before the functions finish, the matrices should be compared with their counterpart that is computed from the other function and then the code continues to run. I was informed that I have to use parallel programming in order to run two functions simultaneously, however, I am not aware of how it works and what line of code I should follow. I appreciate any help, like a simple example or any documentation or notes that help me with this.

Best Answer

In order to do that, you would need to use the Parallel Computing Toolbox, and use the spmd directive. Each lab should use labsend() to send results to the other, and labreceive() to receive values from the other. For example,
p = parpool('local', 2);
spmd
if labindex == 1
... do stuff for first function
stuff_to_send = rand(3,3); %some data to send

labSend(stuff_to_send, 2); %send it

stuff_got = labReceive(2); %get what the other sent

... do something with stuff_got
else
... do stuff for second function
stuff_to_send = randn(5,12); %some data to send
labSend(stuff_to_send, 1); %send it
stuff_got = labReceive(1); %get what the other sent
... do something with stuff_got
end
end