Patch color error "Vectors must be the same length."

조회 수: 7 (최근 30일)
cfy30
cfy30 2020년 6월 29일
댓글: cfy30 2020년 6월 29일
I am trying to plot 2 patchs with different color. " patch(x, y, c, 'FaceColor', 'flat');" shows error "Vectors must be the same length.". How to setup the color?
x =[ 1 2
1 2
2 3
2 3];
y =[1 2
2 3
2 3
1 2];
c=[0 0.7344
0 1.0000
0.5156 0.2656];
figure;
patch(x, y, 'r');
figure;
patch(x, y, c, 'FaceColor', 'flat');
Thanks,
cfy30
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2020년 6월 29일
cfy30 - which of the effects from C - polygon colours are you interested in? Because that will tell you what the dimensions for C should be.
cfy30
cfy30 2020년 6월 29일
Hi Geoff,
I want to set the face color of the patch by the RGB color. Any easy way to do so?
Thanks,
cfy30

댓글을 달려면 로그인하십시오.

채택된 답변

Tommy
Tommy 2020년 6월 29일
The docs linked by Geoff mention that you can specify an n-by-1-by-3 array of RGB values (for n number of faces). I've defined c to be an n-by-3 array of RGB values and then used reshape() to get the dimensions right:
c=[0 0 0.5156 % <- RGB of first color
0.7344 1.0000 0.2656]; % <- RGB of second color
figure;
patch(x, y, reshape(c,[],1,3), 'FaceColor', 'flat');

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 6월 29일
편집: Ameer Hamza 2020년 6월 29일
Using for-loop seems to be the simplest solution in this case.
x =[ 1 2
1 2
2 3
2 3];
y =[1 2
2 3
2 3
1 2];
c=[0 0.7344
0 1.0000
0.5156 0.2656];
figure;
for i=1:size(x,2)
patch(x(:,i), y(:,i), c(:,i).');
end
  댓글 수: 1
cfy30
cfy30 2020년 6월 29일
Thanks Ameer. I used for-loop but want to experiment without it. When I plot up to like 1000 patchs, copy and paste the figure in emf formatting to Word/Powerpoint becomes very big. I want to see if the issue is because of the for-loop used.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by