필터 지우기
필터 지우기

Why is it that Line objects can be instances of two different classes? How can I use findobj to detect only chart primitives?

조회 수: 65 (최근 30일)
Using findobj, I have ended up with two arrays of Line graphics handles,
load handleArrays
hL
hL =
5x1 Line array: Line Line Line Line Line
hD
hD =
5x1 graphics array: Line Line Line Line Line
However, the arrays do not appear to belong to a common class,
whos hL hD
Name Size Bytes Class Attributes hD 5x1 40 matlab.graphics.primitive.Data hL 5x1 40 matlab.graphics.chart.primitive.Line
Moreover, even though hD clearly has all the same properties as hL, it cannot be accessed with standard indexing, e.g.,
isprop(hD(1),'Color')
ans = logical
1
hD.Color
Unrecognized method, property, or field 'Color' for class 'matlab.graphics.primitive.Data'.
What is the function of these two different handle graphics types, and how do I tell findobj to retrieve only the hL kind and not the hD kind?
  댓글 수: 3
Matt J
Matt J 2024년 4월 22일 12:44
편집: Matt J 2024년 4월 22일 12:49
I am using,
h=findobj(ax,'Type','line','-or','Type','functionline');
The current axes was a member of a tiledlayout, but I'm not sure that matters.
Fangjun Jiang
Fangjun Jiang 2024년 4월 22일 13:07
Can you clarify how hD and hL are obtained, and what are you expecting? matlab.graphics.chart.primitive and matlab.graphics.primitive are packages, not class.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2024년 4월 22일 13:14
Handle Graphics objects support the creation of heterogeneous arrays. So for example you can store a figure handle and an axes handle in the same array. [This is more often used to preallocate an array of Handle Graphics objects using gobjects and storing various types of object handles inside.]
f = figure;
ax = axes;
h = [f, ax]
h =
1x2 graphics array: Figure Axes
If you look at the class of the h it is neither Figure nor Axes; it is a common (undocumented) base class from which both Figure and Axes inherit, as per this line from the documentation for heterogeneous arrays I linked to above: "The class of a heterogeneous array is that of the most specific superclass shared by the objects of the array."
class(h)
ans = 'matlab.graphics.Graphics'
What your code shows me is that hD is such a heterogeneous array, something along the lines of h2 in the example below:
L1 = plot(1:10);
hold on
L2 = fplot(@sin);
h2 = [L1; L2]
h2 =
2x1 graphics array: Line FunctionLine
class(h2)
ans = 'matlab.graphics.primitive.Data'
  댓글 수: 2
Matt J
Matt J 2024년 4월 24일 4:57
편집: Matt J 2024년 4월 24일 5:05
Thanks, Steve. However, it is a bit odd to me that a heterogeneous array cannot generate comma-separated lists, and throw a very misleading error message that the property does not exist.
f = figure;
ax = axes;
h = [f, ax]
h =
1x2 graphics array: Figure Axes
h(1).Color
ans = 1x3
1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
h(2).Color
ans = 1x3
1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
h.Color
Unrecognized method, property, or field 'Color' for class 'matlab.graphics.Graphics'.
Steven Lord
Steven Lord 2024년 4월 24일 5:22
It's true that the Figure and Axes classes each define a property named Color. But does their common base class define a Color property?
f = figure;
ax = axes;
h = [f, ax];
properties(h)
No properties for class matlab.graphics.Graphics.
No, their common base class (matlab.graphics.Graphics) has no public properties. So the error message was accurate.
Now if we took two objects that inherit from a common base class that does have properties, like two different types of axes:
f2 = figure;
ax2 = polaraxes;
h2 = [ax, ax2]
h2 =
1x2 graphics array: Axes PolarAxes
class(h2)
ans = 'matlab.graphics.axis.AbstractAxes'
Does this common base class define any public properties?
properties(h2)
Properties for class matlab.graphics.axis.AbstractAxes: Units Position InnerPosition OuterPosition PositionConstraint TightInset Colormap Alphamap ColorOrder ColorOrderIndex LineStyleOrder LineStyleOrderIndex LineStyleCyclingMethod NextSeriesIndex FontUnits FontSize FontSizeMode TitleFontWeight SubtitleFontWeight TitleFontSizeMultiplier SortMethod Clipping NextPlot Toolbar Interactions InteractionOptions Children Parent Visible HandleVisibility Layout
Their common base class has several properties, so we can use one of those to generate a comma-separated list.
h2.Units
ans = 'normalized'
ans = 'normalized'
By default, most of their properties will happen to be the same. One that isn't in this case is Parent.
h2.Parent
ans =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 558 577 433] Units: 'pixels' Use GET to show all properties
ans =
Figure (2) with properties: Number: 2 Name: '' Color: [1 1 1] Position: [671 558 577 433] Units: 'pixels' Use GET to show all properties

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2024년 4월 22일 12:28
편집: Fangjun Jiang 2024년 4월 22일 12:45
load handleArrays.mat
get(hD,'Color')
ans = 5x1 cell array
{[ 0 0 0]} {[ 0 0.4470 0.7410]} {[0.8500 0.3250 0.0980]} {[0.9290 0.6940 0.1250]} {[0.4940 0.1840 0.5560]}
get(hD,'Type')
ans = 5x1 cell array
{'line'} {'line'} {'line'} {'line'} {'line'}
get(hL,'Type')
ans = 5x1 cell array
{'line'} {'line'} {'line'} {'line'} {'line'}
class(hD)
ans = 'matlab.graphics.primitive.Data'
class(hD(1))
ans = 'matlab.graphics.chart.primitive.Line'
class(hL)
ans = 'matlab.graphics.chart.primitive.Line'
class(hL(1))
ans = 'matlab.graphics.chart.primitive.Line'
class([hD,hL])
ans = 'matlab.graphics.primitive.Data'
doc matlab.graphics.primitive.Data
Error using matlab.internal.capability.Capability.require (line 94)
This functionality is not available on remote platforms.

Error in matlab.internal.webwindow (line 161)
Capability.require(Capability.WebWindow);

Error in matlab.internal.doc.ui.CshDocPageHandler/openCshWebWindow (line 87)
cefWindow = matlab.internal.webwindow(url, matlab.internal.getDebugPort());

Error in matlab.internal.doc.ui.CshDocPageHandler/openBrowserForDocPage (line 39)
win = openCshWebWindow(obj, url.string);

Error in matlab.internal.doc.ui.DocPageHandler/openBrowser (line 4)
success = openBrowserForDocPage(obj, url);

Error in matlab.internal.doc.ui.DocPageLauncher/openDocPage (line 30)
success = openBrowser(obj.Handler, getNavigationUrl(activePage));

Error in doc (line 79)
launcher.openDocPage;
In R2023b, it says matlab.graphics.primitive.Data is an undocumented builtin constructor.
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2024년 4월 22일 13:11
In R2023b, it says matlab.graphics.primitive.Data is an undocumented builtin constructor.
doc matlab.graphics.primitive.Data
Fangjun Jiang
Fangjun Jiang 2024년 4월 22일 13:23
For single handle, you can use .Property, like hD(1).Color. But for array of handles, I always use get(). I never thought hD.Color should work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by