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

조회 수: 1 (최근 30일)
Jay Vankawala
Jay Vankawala 2019년 10월 30일
답변: Jyotsna Talluri 2019년 11월 4일
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에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by