%-------------------------------------------------------------------------
% Problem passing an object to parfor
% polynom is a class defined in @polynum, which is located in rundir
% @polynum is defined as in the matlab help under:
% MATLAB/Programming/Classes and Objects/Example - A Polynomial Class
% Art Gleason 21 May 2008
% Bug verified in R2007b, but appears to be fixed in R2008a
% Mayank Advani 29 May 2008
%-------------------------------------------------------------------------

%---define rundir for your system, make sure @polynum is in there---
%rundir = 'C:\users\artg\gtk_survey2008\dct_tests\parfor_object_problem';
rundir = 'W:\artg\gtk_survey2008\dct_tests\parfor_object_problem';
chdir(rundir);

%---define polynom 'p' and vector 'x'---
p = polynom([1 0 -2 -5]);
x = -5:0.1:5;

%---evaluate p(x) using subsref method of class polynom---
y = p(x);

%---evaluate p(x) using a normal for loop---
for i=1:length(x)
  y2(i) = p(x(i));
end

%---plot y and y2, everything fine up to this point---
figure(1); clf; hold on;
plot( x, y, 'b-' );
plot( x, y2, 'og');

%---now try evaluating p(x) using parfor---
% I get an error at this point. Here is a copy of the command window output
% >>close all
% >>clear all
%   serial_eval
%   To learn more about the capabilities and limitations of matlabpool, distributed
%   arrays, and associated parallel algorithms, use   doc matlabpool
%   We are very interested in your feedback regarding these capabilities.
%   Please send it to parallel_feedback@mathworks.com.
%   Submitted parallel job to the scheduler, waiting for it to start.
%   Connected to a matlabpool session with 2 labs.
%   ??? Error using ==> parallel_function>make_general_channel/channel_general at 829
%   Subscript indices must either be real positive integers or logicals.
%   Error in ==> parallel_function>distributed_execution at 741
%       [tags, out] = P.getCompleteIntervals(chunkSize);
%   Error in ==> parallel_function at 553
%   R = distributed_execution(...
%   Error in ==> serial_eval at 33
%   matlabpool open 2
%   Warning: Class ':all:' is an unknown object class.  Element(s) of
%            this class in array '' have been converted to structures.
%   Warning: Class ':all:' is an unknown object class.  Element(s) of
%            this class in array '' have been converted to structures.
% >>
%
%
matlabpool open 2
parfor (i=1:length(x))
  chdir(rundir);   %--not sure if this is required. Same result with/without it
  addpath(rundir); %--not sure if this is required. Same result with/without it
  y3(i) = p(x(i));
end
matlabpool close

%----note that if you comment out the matlabpool commands, it works OK.---
parfor (i=1:length(x))
  y3(i) = p(x(i));
end
plot( x, y3, '.r' );


%---finally, note that I do realize you would not want to run parfor (or
%---even for) in this case. My point is that within parfor the variable
%---'p' somehow loses its status as a polynom object, so methods defined
%---to work on it fail. In my real example I have a lage structure of
%---the class "isahdb" which contains some file pointers and a bunch
%---of other stuff. There is a method of that class that reads a file
%---and this is where it fails. Example of the errors I get are:
%
% We are very interested in your feedback regarding these capabilities.
% Please send it to parallel_feedback@mathworks.com.
%
% Submitted parallel job to the scheduler, waiting for it to start.
% Connected to a matlabpool session with 2 labs.
% ??? Error using ==> isahdbdata.getTraces at 30
% Error using ==> fseek
% Invalid file identifier.  Use fopen to generate a valid file identifier.
%
% Error in ==> parallel_function>distributed_execution at 741
%     [tags, out] = P.getCompleteIntervals(chunkSize);
%
% Error in ==> parallel_function at 553
% R = distributed_execution(...
%
% Error in ==> load_fwf03 at 56
% matlabpool open 2;
%
% Warning: Element(s) of array '' do not match the current constructor
%          definition for class 'isahdb'.  The element(s) have been converted
%          to structures.
% Warning: Element(s) of array '' do not match the current constructor
%          definition for class 'isahdbparam'.  The element(s) have been
%          converted
%          to structures.
% Warning: Element(s) of array '' do not match the current constructor
%          definition for class 'isahdb'.  The element(s) have been converted
%          to structures.
% Warning: Element(s) of array '' do not match the current constructor
%          definition for class 'isahdbparam'.  The element(s) have been
%          converted
%          to structures.
%
%
%---So you can see that the polynom error has to do with not being able
%---to index the variable 'p' and the isahdb error has to do with
%---not being able to read a file, but these are symptoms. The real
%---problem is that within parfor these objects loose their class
%---and therefore lose their associated methods.