Assigning different patch objects to different subplots of the same figure in a conditional for loop

10 views (last 30 days)
I'm creating two subplots per figure in a conidtional for loop. After plotting the data points on the subplots, I want to assign 2 different patch objects to each of the subplots.
The problem is that whereas the first patch object is only assigned to the first subplot as expected, the second patch object is assigned to both, the first and second subplots. So I end up having overlapped patches on the first subplot, but only one patch on the second subplot.
Please look at the resulting figure for clarification, and also the code I use. Thank you.
for j=1:2:jj;
figure(j);
p1=subplot(2,1,2);
C_Iprofile=plot(C_spatial_mean(:,:,j));
Max_C_data_I=max(C_spatial_mean(:,:,j))+(20*std(MultiTrialMean)/150);
Min_C_data_I=min(C_spatial_mean(:,:,j))-(20*std(MultiTrialMean)/150);
axis([0 6100 Min_C_data_I Max_C_data_I]);
p2=subplot(2,1,1);
V_Iprofile=plot(V_spatial_mean(:,:,j));
Max_V_data_I=max(V_spatial_mean(:,:,j))+(20*std(MultiTrialMean)/150);
Min_V_data_I=min(V_spatial_mean(:,:,j))-(20*std(MultiTrialMean)/150);
axis([0 6100 Min_V_data_I Max_V_data_I]);
if ismember(j,[1:5*SkipFileNo:jj]);
p3=patch([LEDinitTrial1 (LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur) LEDinitTrial1],[Min_V_data_I*[1 1] Max_V_data_I*[1 1]], [0.8 0.8 0.8 0.8]);
p4=patch([(LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur)],[Min_V_data_I*[1 1] Max_V_data_I*[1 1]], [0.8 0.8 0.8 0.8], '-y');
p5=patch([LEDinitTrial1 (LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur) LEDinitTrial1],[Min_C_data_I*[1 1] Max_C_data_I*[1 1]], [0.8 0.8 0.8 0.8]);
p6=patch([(LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur)],[Min_C_data_I*[1 1] Max_C_data_I*[1 1]], [0.8 0.8 0.8 0.8], '-y');
else if
%....some code
end
end
alpha(0.1);
copyobj(p3,p2)
copyobj(p4,p2)
copyobj(p5,p1)
copyobj(p6,p1)
end

Answers (1)

Voss
Voss on 23 Dec 2021
This code creates 4 patches (p3, p4, p5, p6) in one axes (p2), then makes a copy of patches p3 and p4 in axes p2 and makes a copy of patches p5 and p6 in axes p1.
Instead of making the copies via copyobj, try creating the patches directly in the axes they belong in, using the syntax:
p3=patch(p2,[LEDinitTrial1 (LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur) LEDinitTrial1],[Min_V_data_I*[1 1] Max_V_data_I*[1 1]], [0.8 0.8 0.8 0.8]);
p4=patch(p2,[(LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur)],[Min_V_data_I*[1 1] Max_V_data_I*[1 1]], [0.8 0.8 0.8 0.8], '-y');
p5=patch(p1,[LEDinitTrial1 (LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur) LEDinitTrial1],[Min_C_data_I*[1 1] Max_C_data_I*[1 1]], [0.8 0.8 0.8 0.8]);
p6=patch(p1,[(LEDinitTrial1+LEDdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur+Airdur) (LEDinitTrial1+LEDdur)],[Min_C_data_I*[1 1] Max_C_data_I*[1 1]], [0.8 0.8 0.8 0.8], '-y');
and don't call copyobj at all. See the documentation for patch for more information about this syntax.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!