Info

This question is closed. Reopen it to edit or answer.

setting fit options doesn't work with a parfor loop

1 view (last 30 days)
I am performing a fit multiple times within a for loop. When I use a normal "for" loop, everything works fine. But when I use a "parfor" loop, I get an error related to setting the fit options. Below is a minimum working example. I am using Matlab R2018b.
% 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

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!