Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab - customize surface color depending on a parameter

I have a problem in customizing colors of a surface plot (trisurf or trimesh).

I would like every face to have a color depending on the relative strain value (calculated for every triangle of the mesh). I was thinking about something like:

p = patch('Faces',faces,'Vertices',verts,'FaceColor',strain);

But FaceColor seems not to work with arrays.

strain is an array of Nx1 where N is the number of faces.

like image 495
Benedetto Roda Avatar asked Jan 29 '26 05:01

Benedetto Roda


1 Answers

First you need to change FaceColor to 'flat' to enable colours to be read from a color-data array - the CData property:

Try this (not tested though):

p = patch( ...
   'Faces',     faces, ...
   'Vertices',  verts, ...
   'FaceColor', 'flat', ...
   'CData',     strain' ...
);

Here I set the CData property to the transpose of your strain vector. MATLAB should then automatically map the N strain-values in this vector to the chosen colormap (linearly). See property CDataMapping for more information:

Documentation:

http://www.mathworks.se/help/matlab/ref/patch_props.html#FaceColor http://www.mathworks.se/help/matlab/ref/patch_props.html#CData http://www.mathworks.se/help/matlab/ref/patch_props.html#CDataMapping

like image 50
Ole Thomsen Buus Avatar answered Jan 31 '26 22:01

Ole Thomsen Buus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!