So I build a class called 'realization', from which working with individual objects works fine.
Now, from a script, stored in the same folder as the classdef file, I want to initialize objects of the class in the following way
parfor Fit=1:length(powering)
traject(Fit,relnr)=realization(powering(Fit),U,gamma,delta);
traject(Fit,relnr)=traject(Fit,relnr).pcountrun();
traject(Fit,relnr)=traject(Fit,relnr).homodynerun();
Where powering is a short array of numbers,Nrel an integer and U,gamma,delta plain numbers. Previously, I did make a very similar construction in the parfor before switching to OO-programming and that one seemed to work out fine. However, when trying to execute the piece of code above, I receive an error
An UndefinedFunction error was thrown on the workers for 'traject'. This might be because the file containing 'traject' 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.
Undefined function 'traject' for input arguments of type 'double'.
First of all, I never intended 'traject' to be a function, just a matrix of realization-objects. But in my non OO-implementation, MATLAB did use it the way I intended, so I wouldn't think the assignment is improper use of parfor by itself.
What I have already tried:
addAttachedFiles(gcp,'realization.m')
just before the for-loop, to ensure every worker has access to the class definition. This doesn't make a difference, though.
function thisRealization=realization(f,u,gam,delt)
thisRealization.gamma=gam;
thisRealization.delta=delt;
such that if there are zero arguments, the condition nargin==4 is not satisfied and MATLAB returns to the default constructor.
- Ordinary for-loop instead of parfor works fine (this was a question from Matt J).
As a final note I am a beginner to OO-programming in MATLAB and also fairly new with regard to parallel computations. Therefore, I used a Value class in order not to complicate things too much from the start, but I may consider switching to Handle class later if that is faster (methods pcountrun and homodynerun are computationally expensive and produce large matrices of data as class properties). If you would already recommend me to switch to handle classes already because it matches better with parfor, I can do that though.