# plot with for loop

2 views (last 30 days)
Enzo on 4 Nov 2022
Answered: Kevin Holly on 5 Nov 2022
Hello everyone,
i have a 3D matrix, which where each colums represent a different channel (1:32), each rows is a single time point and the third dimension is a set of repeated stimulus (or trials, more or less 120). the final matrix is a a 2000x 32x120.
I would like to have the same plot you see in the code below, repeated every time for each comlums listed in the variable "range_of_chan = (10:20)" -> as for this example, I would like to get 21 different plots. Moreover, I would like that any of this plot to have a name according to the channel being plotted (ex: Single channel mean LFP n° 17) and so on. The same goes for the saving.
Maybe, the easiest way would be to have a for loop running. Could anyone of you give me some piece of code suitable for this case?
Thanks so much in advance for any help!
%% mean LFP [use TD_32 to plot]
mean_LFP_fil = mean(TD_32, 3);
%% features extraction and single channel selection [plot 2, 3]
range_of_chan = (10:20);
select_ch = 17; % type the channel of interest
[M,I] = min(mean(TD_32(:,select_ch,:),3));% principal peak amplitude and latency
%% plot 2 A [single channel mean LFP]
pr_peak_amp = M*10e13;
pr_peak_lat = I;
fig_name_3 = 'Single channel mean LFP';
figure(3)
plot(Time_vec, mean(TD_32(:,select_ch,:),3))
title(fig_name_3)
hold on
plot(get(gca,'xlim'), [0 0], 'k--')
plot([0 0], get(gca,"YLim"), 'k--')
plot([0 0]+.5,get(gca,'ylim'),'k--')
xlabel('Time (s)'), ylabel('activity (\muV)x100')
xline(5000,'--r')
text(I, 0, sprintf('Principal peak latency: %f ', pr_peak_lat))
plot(I, M,'-','MarkerSize',8,'Color','b')
text(I, M, sprintf('Principal peak amplitude: %f ', pr_peak_amp))
plot(I, M,'o','MarkerSize',6,'Color',[1 0 0])
set(gca,"XLim")
saveas(gcf,[main_folder,subfolder,fig_name_3],'png')

Kevin Holly on 5 Nov 2022
Here is how to do a for loop:
main_folder = pwd;%uigetdir;
subfolder = 'foldername';
% Making up values so I can show preview plot
TD_32 = rand(2000,32,120)-0.5;
Time_vec = 1:8000/2000:8000;
%% mean LFP [use TD_32 to plot]
mean_LFP_fil = mean(TD_32, 3);
for trial = 1:size(TD_32,3)
%% features extraction and single channel selection [plot 2, 3]
for channel = 10:20
[M,I] = min(mean(TD_32(:,channel,:),3));% principal peak amplitude and latency
%% plot 2 A [single channel mean LFP]
pr_peak_amp = M*10e13;
pr_peak_lat = I;
fig_name_3 = ['Channel ' num2str(channel) ' mean LFP' ' Trial ' num2str(trial)];
figure(3)
plot(Time_vec, mean(TD_32(:,channel,:),3))
title(fig_name_3)
hold on
plot(get(gca,'xlim'), [0 0], 'k--')
plot([0 0], get(gca,"YLim"), 'k--')
plot([0 0]+.5,get(gca,'ylim'),'k--')
xlabel('Time (s)'), ylabel('activity (\muV)x100')
xline(5000,'--r')
text(I, 0, sprintf('Principal peak latency: %f ', pr_peak_lat))
plot(I, M,'-','MarkerSize',8,'Color','b')
text(I, M, sprintf('Principal peak amplitude: %f ', pr_peak_amp))
plot(I, M,'o','MarkerSize',6,'Color',[1 0 0])
set(gca,"XLim")
saveas(gcf,[main_folder,filesep,subfolder,filesep,fig_name_3],'png')
end
end
0×0 empty cell array

### Categories

Find more on Signal Operations in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!