MATLAB: Running two sections of Matlab code in parallel. is this possible

parallel computingsections

I have a Matlab code that I need to run two sections at the same time as part of a control system.
It roughly looks like this
%%Section 1
logs data during specified time and inputted to a control system
%%Section 2
takes photos and information from photo is also used in the control system
is this possible and if so how?

Best Answer

It is generally possible using the Parallel Computing toolbox.
parpool(2)
spmd
if labidx == 1
collect_for = 15; %minutes
start_time = now; %days

end_time = start_time + collect_for/60 / 24; %days
while now < end_time
data_from_camera = labReceive(2);
%use the data from the camera to figure
%out how to control, send commands to the
%control system, and read log data from the
%control system
...
end
elseif labidx == 2
... set up camera
while true
... grab a picture
labSend(1, image_from_camera);
end
end
end
That said: it is common that you can do either data collection from the instrument or taking the pictures asynchronously, and have the availability of data signaled by a callback routine being called. When you can use this approach, you only need a single MATLAB proccess, as the interrupts and buffer management are handled by MATLAB.
Also note: you cannot update the graphics display from any worker (parfor, spmd, batch, parfeval). If you need to update the graphics display then you need to approach the parallelism slightly differently. The techniques became a bit easier in R2017a.