|On this page…|
In general, if you define a patch with Faces and Vertices data and then want to modify its data, you should continue to use these same properties. Do not switch modes and modify the XData, YData, ZData, or CData properties.
If you specify a patch with Faces and Vertices data, MATLAB® constructs arrays of data for the XData, YData, ZData and CData properties when you query these properties. However, these arrays contain only enough data to define the same number of vertices as there are referred to in the Faces property.
If the number of vertices in the Vertices property is greater than the number of vertices used by the Faces property, then MATLAB plotting functions cannot generate complete x, y, and z data from the faces and vertex data.
While you should not use mixed data specification when defining patch objects directly, you might need to modify patch data when using functions that create patch objects. For example, the bar function creates patch objects to implement the bars in a graph.
Note: The barseries YData property enables you to modify the bar graph without the need to use the following steps. See the bar function for more information on working with bar graphs.
The bar function uses y-data values to determine the height of each bar, but creates each bar as the face of a patch specified by faces and vertices. For example,
rng(0,'twister') h = bar(rand(10,1)); % y data for each bar p = get(h,'children'); % get the handle of the patch cl = get(gca,'CLim');
Before you can change the patch YData property, you must switch the patch to x, y, and z data as follows:
xd = get(p,'XData'); yd = get(p,'YData'); zd = get(p,'ZData'); cd = get(p,'CData'); set(p,'XData',xd,'YData',yd,'ZData',zd,'CData',cd); set(gca,'CLim',cl)
Setting the XData, YData, ZData and CData properties causes the patch function to match the faces and vertex data with x, y, and z data. Because there is a change in the patch data, the color limits change, so you must use the original values for the axes CLim property.
You can now modify the y data values to change your graph. For example, the value of bar at x = 6 is 0.0975:
yd(:,6) % vertices of patch defining sixth bar ans = 0 0.0975 0.0975 0
You can change this bar by changes rows 2 and 3 in the y data for column 6. For example, set the height of the bar equal to the average of bar 5 and bar7:
% Use value from row 2 to calculate mean yd(2:3,6) = mean([yd(2,5),yd(2,7)]);
Now reset the patch YData property:
Another reason you might want to modify face-vertex data for bar graphs or other objects is to modify their CData to customize how they are colored. Techniques for doing this for 3–D bar graphs are explained in Color 3-D Bars by Height in the MATLAB Graphics documentation.