Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Modifying Data on Existing Patch Objects

Specifying Patch Data

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.

Handling Mixed Data Specification

When you create a patch specified with Faces and Vertices data, arrays of data for the XData, YData, ZData and CData properties are constructed when you query them. 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 themselves create patch objects. For example, the bar function creates patch objects to implement the bars in a graph.

The 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,

rand('state',4)
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)

This setting of 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 = 10 is 0.0122:

yd(:,10)
ans =
         0
    0.0122
    0.0122
         0

You can change this bar by changes rows 2 and 3:

yd(2:3,10) = [.65 .65];

Now reset the patch YData property:

set(p,'YData',yd)

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 2–D and 3–D bar graphs are explained in Coloring 2-D Bars According to Height and Coloring 3-D Bars According to Height in the MATLAB Graphics documentation.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS