Using 'plotResiduals' in a for loop.. is it possible to using indexing to store the results and plot then together?

1 次查看(过去 30 天)
I have a series of data fits I've performed using 'fitnlm' which I've acquired using a for loop like so.
fittables = cell(1, numel(files));
modelfun = @(sigma,x) exp(-x.^2 / (2 * sigma));
for k = 1:length(files)
table{k} = table(x{k}, y{k}, 'VariableNames', {'x', 'y'});
beta0 = 1;
mdl{k} = fitnlm(table{k}, modelfun, beta0);
end
I want to use the 'plotResiduals()' function to plot the residuals of each of these fits onto one graph. I can get it to work on a single fit but I can't work out how to index the function. I've tried the following two ways..
residuals{k} = plotResiduals(mdl{k},'caseorder');
which will run but it just overwrites the plot with each iteration,
and
residuals(k) = plotResiduals(mdl(k),'caseorder');
which results in the error "Undefined function 'plotResiduals' for input arguments of type 'cell' ".
Is there any way to plot these residuals on the same plot within the for loop? Indexing doesn't seem to be working.
  2 个评论
mel1708
mel1708 2020-1-9
Here is the whole plotting section of the code which is inside the for loop. I did include hold on and hold off as you can see. Should it matter that the other plots are in between hold on and hold off? Or should I be starting a new hold on and hold off section for plotting the residuals?
figure(11);
hold on
plot(dose{k},sf{k},'Color',col(k,:),'Marker','x','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;
title('Rayleigh Distribution Fit - Nonlinear Regression Model');
plot(dose{k},yfitRayl{k},'Color',col(k,:),'HandleVisibility','off');
set(gcf,'color','w');
figure(12);
resid(k) = plotResiduals(mdlRayl(k),'caseorder');
hold off

请先登录,再进行评论。

采纳的回答

Chidvi Modala
Chidvi Modala 2020-1-30
It appears that plotResidual respects "hold on" for other than probability plots. You can create the probability plot directly using normplot, and not using probplot or plotResiduals. For example:
load carsmall
tbl = table(MPG,Weight);
tbl.Year = categorical(Model_Year);
mdl = fitlm(tbl,'MPG ~ Year + Weight^2');
normplot(mdl.Residuals.Raw);
hold on;
h1= plotResiduals(mdl)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by