MATLAB Answers

0

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

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?

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: 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.

  0 件のコメント

サインイン to comment.



Translated by