MATLAB: Using getsnapshot in parfor function

getsnapshotparfor

Hello all,
I am trying to run this following code in parallel but got this error:
Error using realTime_pointGrey>(parfor body) (line 52)
An UndefinedFunction error was thrown on the workers for 'getsnapshot'. This might be because the file containing 'getsnapshot' is not accessible on the workers. Use
addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Error in realTime_pointGrey (line 50)
parfor id = 1:4
Caused by:
Undefined function 'getsnapshot' for input arguments of type 'struct'.
I am not very familiar with using addAttachedFiles, could you please help me?
The code I am working on:
parfor id = 1:4
% Get the snapshot of the current frame
data(:,id) = reshape(double(rgb2gray(getsnapshot(vid))),size(data0,1)*size(data0,2),1);
end
data_mean = mean(data,2);

Best Answer

This error appears to be misleading - I think the problem is that the object vid is not being transferred to the workers correctly. You could confirm this like so
class(vid) % class on the client
spmd
class(vid) % class on the workers
end
If that isn't the same thing (and I strongly suspect it isn't), then what you need to do is create vid on the workers. In other words, I would first try
spmd
vid = <create 'vid'>;
getsnapshot(vid);
end
If that works, then one approach you might take to working with vid is to use parallel.pool.Constant as described here. (Or Worker Object Wrapper for older MATLAB releases).
Related Question