1 view (last 30 days)

I have a cell array with multiple data sets like the following..

Dose (Gy) SF

0.48300 0.73753

0.99124 0.50232

2.01313 0.33210

2.99717 0.19733

4.01905 0.10447

6.03759 0.01870

So I have imported several of these data sets and I've them under the names dose{k} and sf{k} and I am performing operations on them using a for loop over k. What I am having trouble with is fitting a Rayleigh distribution (i.e. an equation) to each data set using nonlinear regression (fitnlm or nlinfit commands) by looping over k. This is my code..

% ==============================

% RAYLEIGH DISTRIBUTION

%==============================

for k = 1:length(theFiles)'

tbl = table(dose{k}, sf{k});

modelfun = @(sigma2,x) exp(-x.^2 / (2 * sigma2(1)));

beta0 = 1;

mdlRayl{k} = nlinfit(dose{k},sf{k}, modelfun, beta0);

%mdlRayl{k} = fitnlm(tbl, modelfun, beta0);

sigma2Estimate{k} = mdlRayl{k}.Coefficients{:, 'Estimate'}

sigma{k} = sqrt(sigma2Estimate{k});

yfitRayl{k} = (dose{k} / sigma{k}.^2) * exp(-dose{k} / (2 * sigma{k}^2));

figure(11);

hold on

plot(dose{k},sf{k},'o','Color',col(k,:),'Marker','o','LineStyle','none','DisplayName',txt);

h = errorbar(dose{k},sf{k},errs{k},'Marker','none','LineStyle','none','Color',[col(k,1), col(k,2), col(k,3)],'HandleVisibility','off');

legend('Location','southoutside');

xlabel('Dose (Gy)'); ylabel('Surviving Fraction'); grid on; legend show;

plot(dose{k},yfitRayl{k},'Color',col(k,:),'HandleVisibility','off');

end

hold off

It has many problems.. it won't let me index tbl so I can have a whole new table everytime I perform a fit for each k, and therefore the fit is not being performed on each data set. Is anyone able to see where I am going wrong?

Guillaume
on 6 Jan 2020

For the problem with the tables:

Before the loop:

fittables = cell(1, numel(theFiles)); %preallocate cell array to store all the tables. Use a more descriptive name than just tbl

for k = 1:numel(theFiles) %Prefer numel to length. You had a transpose here which was completely pointless

fittables{k} = table(dose{k}, sf{k}, 'VariableNames', {'Dose', 'SF'}); %I'd recommend overriding the default variable names

%...

end

I don't have the required toolbox so can't really help with the rest. These lines look suspicious:

mdlRayl{k} = nlinfit(dose{k},sf{k}, modelfun, beta0);

sigma2Estimate{k} = mdlRayl{k}.Coefficients{:, 'Estimate'};

According to the doc, nlinfit returns a vector, so mdlRayl{k} would be a vector, which can't be indexed with .Coefficients. .Coefficients{:, 'Estimate'} imply that mdlRayl{k} should be a table.

Note: instead of having one table per file, all stored in a cell array you may find it easier to have just one table with an additional variable such as FileNo. There are many aggregration functions in matlab that allow you to perform the same operation to grouped data, eg. grouptransform.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499039-nonlinear-regression-fits-on-data-sets-in-a-cell-array#comment_783332

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499039-nonlinear-regression-fits-on-data-sets-in-a-cell-array#comment_783332

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499039-nonlinear-regression-fits-on-data-sets-in-a-cell-array#comment_783349

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499039-nonlinear-regression-fits-on-data-sets-in-a-cell-array#comment_783349

Sign in to comment.