MATLAB Answers

Matlab Trisurf Color By Face

13 views (last 30 days)
I have looked and found some workarounds for this, but never an explanation why it doesn't work. The Matlab documnetation directly says "If you want to use true colors, then specify FaceVertexCData in one of these forms: ... For one color per face, use an m-by-3 array of RGB triplets, where m is the number of rows in the Faces property". I have an array of m-by-3 RGB values, but it still throws this error:
Warning: Error creating or updating Patch
Error in value of property FaceVertexCData
Number of colors must equal number of vertices or faces
When you click on the "FaceVertexCData", it brings you to the documentation where I pulled that excerpt from (Patch Properties > FaceVertexCData). So what gives? The documentation says I can individually color the faces, but it doesn't seem to let me. Also, I know the below code does not color faces differently. This is a minimal reproducible example, not may actual code. I would edit rows of the colors matrix to different colors to actually color them differently.
vertices = [0, 0, 0;
1, 0, 0;
1, 1, 0;
0, 1, 0;
0, 0, 1;
1, 0, 1;
1, 1, 1;
0, 1, 1];
[faces, volume] = convhull(vertices(:,1),vertices(:,2),vertices(:,3));
colors = repmat([0 0 1],size(faces,1),1);
figure('Name', 'Colored Differently');
trisurf(faces,vertices(:,1),vertices(:,2),vertices(:,3),colors,'EdgeColor',[0 0 1], 'DisplayName', 'Convex Hull')
figure('Name', 'Colored the Same');
trisurf(faces,vertices(:,1),vertices(:,2),vertices(:,3),'FaceColor',[0 1 0],'EdgeColor',[1 0 0], 'DisplayName', 'Convex Hull')

Accepted Answer

Uday Pradhan
Uday Pradhan on 11 Nov 2020
Hi John,
This error occurs because whenever we call:
the function operates in the "indexed" mode i.e. it treats "colors" as a m - by - 1 column vector, where m is either equal to number of faces or vertices. Hence number of rows in colors is taken as 12 x 3 = 36 and not 12 as you are expecting. To get around this I would suggest the following solution:
a = trisurf(faces,vertices(:,1),vertices(:,2),vertices(:,3));
a.FaceVertexCData = colors;
Hope this helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!