I am performing a fit of data multiple times using a loop. When I use a "for" loop, everything works as expected. However, if I use a "parfor" loop, I get an error. If I remove the fit options (fo), then the parfor loop works as expected. Does anyone know why the parfor loop generates an error when I specify the fit method using the fitoptions?
% Generate some data
Npts = 50;Ntime = 10;tau = 10;x = repmat( (1:Npts)',1,Ntime);y = exp(-x/tau) + (rand(Npts,Ntime)-0.5)/10;% Plot the data
figure;plot(x(:,1),y(:,1),'-o')title('Data for first time step')% Fit options
ft = fittype('exp1');fo = fitoptions( 'Method', 'NonlinearLeastSquares' );% Pre-allocate the decay constant, tau
tau = NaN(1,Ntime);parfor q = 1:Ntime % Perform the exponential fit at time step q
fitresult = fit( x(:,q), y(:,q), ft, fo); % Save the decay constant, tau
tau(q) = -1/fitresult.b; end
Best Answer