Azzera filtri
Azzera filtri

Nonlinear regression fits on data sets in a cell array?

2 visualizzazioni (ultimi 30 giorni)
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?
  2 Commenti
KSSV
KSSV il 6 Gen 2020
Modificato: KSSV il 6 Gen 2020
Try tbl{k} instead of tbl(k). By the way tbl is not being used anywhere.
mel1708
mel1708 il 6 Gen 2020
I actually copied the version of the code I was playing around with earlier today, oops! I've edited the question now so it's got the right version of the code I'm asking about.
I have tried brace indexing tbl but I end up getting the error message "Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript."

Accedi per commentare.

Risposte (1)

Guillaume
Guillaume il 6 Gen 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.

Community Treasure Hunt

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

Start Hunting!

Translated by