Multiple Figure Output from if/then - how to get onto one subplot?

2 次查看(过去 30 天)
This code fits a line to some data. When it fits certain perameters we set (r, b), it graphs the output.
Each iteration of a for loop (defined) has multiple graphs that fit these perameters. Right now, the code is opening a new window for each new figure. I would like it to instead arrange all the graphs into one subplot.
fit_thresh = .25 ; %set a threshold for the fit of the linear regression, just randomly picked this number, could be more stringent
%could change the fit_thresh
for c = 1:size(mean_fr,1)
%Then do a simple linear regression, look for a good fit and a
%positive slope
x = 1:numel(smoothdata(bin_spikes(c,:),'movmean',5));x=x'; %X in this case is just time (so 1 : number of bins)
X = [ones(length(x),1) x];
y = smoothdata(bin_spikes(c,:),'movmean',5); y=y';
b = X\y ; % can use "mldividde" instead of "\"
yfit = X *b ; %the line that is fit
%assess fit using an R^2 measure
r = 1 - sum((y - yfit).^2)/sum((y - mean(y)).^2) ; %goes from 0 to 1, with 1 being perfect fit
if r > fit_thresh
if b > 0 %looking for positive slope, could eventually put a threshold here
thisfig = figure();
plot(smoothdata(bin_spikes(c,:),'movmean',5)) %look for positive correlation, on the smoothened, binned data
title(['Channel ' num2str(recording_info.channel_numbers(c)) ' Area ' recording_info.area{c} ])
fig = gca;
fig.XTickLabel = num2cell(fig.XTick *10);
fig.XLabel.String = 'Time (ms) after sample offset';
fig.YLabel.String = 'FR (spikes/s)';
hold on
plot (x, yfit, 'r-.');
hold off
end
It probably requires some sort of for loop with the subplot function, but how should it be structured? Where should I put it?

回答(1 个)

Jyotsna Talluri
Jyotsna Talluri 2019-11-4
Specify the subplot and hold on at the starting of for loop and hold off at the end of for loop.If size(mean_fr,1) is assumed to be 10,consider subplot(2,5,c);
fit_thresh = .25 ; %set a threshold for the fit of the linear regression, just randomly picked this number, could be more stringent
%could change the fit_thresh
for c = 1:size(mean_fr,1)
subplot(2,5,c);
hold on;
%Then do a simple linear regression, look for a good fit and a
%positive slope
x = 1:numel(smoothdata(bin_spikes(c,:),'movmean',5));x=x'; %X in this case is just time (so 1 : number of bins)
X = [ones(length(x),1) x];
y = smoothdata(bin_spikes(c,:),'movmean',5); y=y';
b = X\y ; % can use "mldividde" instead of "\"
yfit = X *b ; %the line that is fit
%assess fit using an R^2 measure
r = 1 - sum((y - yfit).^2)/sum((y - mean(y)).^2) ; %goes from 0 to 1, with 1 being perfect fit
if r > fit_thresh
if b > 0 %looking for positive slope, could eventually put a threshold here
thisfig = figure();
plot(smoothdata(bin_spikes(c,:),'movmean',5)) %look for positive correlation, on the smoothened, binned data
title(['Channel ' num2str(recording_info.channel_numbers(c)) ' Area ' recording_info.area{c} ])
fig = gca;
fig.XTickLabel = num2cell(fig.XTick *10);
fig.XLabel.String = 'Time (ms) after sample offset';
fig.YLabel.String = 'FR (spikes/s)';
plot (x, yfit, 'r-.');
end
I hope this works fine.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by