catalogue
Object properties and dot notation
Append data to attribute value
Drawing object variables are handles
Modify properties using set and get
Multi object / attribute operation
Object properties and dot notation
The graph function returns one or more objects created by the function. For example:
h = plot(1:10);
h refers to lines drawn in the drawing with values from 1 to 10.
The dot notation syntax uses object variables and case sensitive attribute names to connect with dots (.) to form the object dot name notation:
object.PropertyName
If the object variable is non scalar, the index is used to refer to a single object:
object(n).PropertyName
Scalar object variable
If h yes plot Function, then h.Color Is specific to the line Color Attribute value:
h.Color ans = 0 0.4470 0.7410
If you assign a color value to a variable:
c = h.Color;
Variable c is a double precision value.
whos Name Size Bytes Class c 1x3 24 double h 1x1 112 matlab.graphics.chart.primitive.Line
You can use the assignment statement to change the of the line Color Attribute value:
h.Color = [0 0 1];
Use dot notation attribute references in expressions:
meanY = mean(h.YData);
Or change attribute values:
h.LineWidth = h.LineWidth + 0.5;
Use multiple dot references to refer to other objects contained in the property:
h.Annotation.LegendInformation.IconDisplayStyle ans = on
Set the object properties contained in the properties:
ax = gca; ax.Title.FontWeight = 'normal';
Non scalar object variable
The graph function returns an array of objects. For example:
y = rand(5); h = plot(y); size(h) ans = 5 1
Use the array index to access the line representing the first column in y:
h(1).LineStyle = '--';
Use the set function to set the of all lines in the array LineStyle:
set(h,'LineStyle','--')
Append data to attribute value
With dot notation, you can use the "end" index to append data to attributes that contain an array of data, such as lines XDATA and YData . For example, this code is updated at the same time XData and YData to extend the line. You must ensure that the x and y data of the line are the same size before you can render or return to MATLAB by calling drawnow ® Prompt.
h = plot(1:10); for k = 1:5 h.XData(end + 1) = h.XData(end) + k; h.YData(end + 1) = h.YData(end) + k; drawnow end
Drawing object variables are handles
The object variable returned by the graph function is a handle. A handle is a reference to an actual object. Object variables are handles that have special behavior when copying and deleting objects.
Copy object variables
For example, create a drawing with one line:
h = plot(1:10);
Now copy the object variable to another variable and set the property value with the new object variable:
h2 = h; h2.Color = [1,0,0]
Assigning the object variable H to H2 creates a copy of the handle instead of the object referenced by the variable. Slave variable h Visited Color Attribute values and dependent variables h2 Access is the same.
h.Color ans = 1 0 0
h and h2 refer to the same object. Copying handle object variables does not copy objects.
Delete object variable
Two object variables in the workspace reference the same line.
whos Name Size Bytes Class h 1x1 112 matlab.graphics.chart.primitive.Line h2 1x1 112 matlab.graphics.chart.primitive.Line
Now close the window that contains the line diagram:
close gcf
The line object does not exist, but the object variable referencing this line still exists:
whos Name Size Bytes Class h 1x1 112 matlab.graphics.chart.primitive.Line h2 1x1 112 matlab.graphics.chart.primitive.Line
But this object variable is no longer valid:
h.Color Invalid or deleted object. h2.Color = 'blue' Invalid or deleted object.
To remove invalid object variables, use clear:
clear h h2
List object properties
To see which properties an object contains, use the get function:
get(h)
MATLAB returns a list of object attributes and their current values:
AlignVertexCenters: 'off' Annotation: [1x1 matlab.graphics.eventdata.Annotation] BeingDeleted: 'off' BusyAction: 'queue' ButtonDownFcn: '' Children: [] Clipping: 'on' Color: [0 0.4470 0.7410] ... LineStyle: '-' LineWidth: 0.5000 Marker: 'none' ...
You can use the set function to view property values with a set of enumerated values:
set(h,'LineStyle') '-' '--' ':' '-.' 'none'
To display all settable properties, including possible property values with a set of enumerated values, use set and object variables:
set(h)
Modify properties using set and get
You can also use set and get functions to access and modify properties.
The basic syntax for setting an existing object property value is:
set(object,'PropertyName',NewPropertyValue)
To query the current value of a specific object property, use a statement of the following form:
returned_value = get(object,'PropertyName');
Property names are always character vectors. You can use variables in the form of single quotes or character vectors. The attribute value depends on the specific attribute.
Multi object / attribute operation
If the object parameter is an array, MATLAB sets specific values for all identified objects. For example:
y = rand(5); h = plot(y);
Set all lines to red:
set(h,'Color','red')
To set the same property on multiple objects, use a struct or cell array to specify the property name and value. For example, define a structure and correctly set the coordinate area properties to display a specific drawing:
view1.CameraViewAngleMode = 'manual'; view1.DataAspectRatio = [1 1 1]; view1.Projection = 'Perspective';
To set these values in the current coordinate area, enter:
set(gca,view1)
Query multiple attributes
You can define a cell array of attribute names and use it to get those attribute values. For example, suppose you want to query the value of the "camera mode" attribute of the coordinate area. First, define the cell array:
camModes = {'CameraPositionMode','CameraTargetMode',... 'CameraUpVectorMode','CameraViewAngleMode'};
Use this cell array as a parameter to get the current values of these properties:
get(gca,camModes) ans = 'auto' 'auto' 'auto' 'auto'