Hey Rui,
To send the “myclass” class that you defined, to the worker’s workspace, you can use the “addAttachedFiles” function. The documentation for the function can be found at the following link:
Here is an example of using the function with a pool of 3 workers. The “myclass” is the same as you have provided, but with “variable1” added as property of the class. We then create the pool, then add the class file to it and then every worker creates an object of the class:
assignin('base','theClass',theClass)
addAttachedFiles(pool,{'myclass.m'})
disp(['Worker ', num2str(spmdIndex), ' myclass objects value of variable1: ', num2str(obj.variable1)]);
We get the output as follows:
However, if you don’t want to send the class file to the workers and instead want to copy the object between the worker’s worspace, then we can use “parallel.pool.Constant” method. The documentation for the method can be found in the link below:
Using this, you can share copies of the object itself to the worker’s workspace. The following is a code snippet that does this:
distributedObj = parallel.pool.Constant(theClass);
obj = distributedObj.Value;
disp(['Worker ', num2str(spmdIndex), ' accessing myClass variable1: ', num2str(obj.variable1)]);
We get the output as follow: