Class returns wrong type for patch return variable assigned to an array within a class

조회 수: 1 (최근 30일)
Within a class I have an array to store a number of pacthes:
classdef Draw3DClass < handle
properties
Frame
colors
patchs
lines
transform;
transform_function;
vector_z_function;
vector_z_name;
vector_z_scale;
end
methods......
I'm having problems within the class to get the patch array field component types. From the debugger (stopped within a method of the class assigning the pactches) I get this output
......
K>> p=patch(ax,fv(i))
p =
Patch with properties:
FaceColor: [0 0 0]
FaceAlpha: 1
EdgeColor: [0 0 0]
LineStyle: '-'
Faces: [8×4 double]
Vertices: [18×3 double]
Show all properties
K>> class(p)
ans =
'matlab.graphics.primitive.Patch'
K>> obj.patchs(i)=p
obj =
Draw3DClass with properties:
Frame: []
colors: [7×4 double]
patchs: 11.0012
lines: []
transform: 10.0029
transform_function: @abs_transform
vector_z_function: []
vector_z_name: []
vector_z_scale: []
K>> class(obj.patchs(i))
ans =
'double'
If I play with the same idea using a plain array of patches, that is not a field of anything, it seems that I get type reported correctly
K>> clear a
K>> a(1).b=p
a =
struct with fields:
b: [1×1 Patch]
K>> a(1).b(2)=p
a =
struct with fields:
b: [1×2 Patch]
K>> class(p)
ans =
'matlab.graphics.primitive.Patch'
K>> class(a(1))
ans =
'struct'
K>> class(a(1).b)
ans =
'matlab.graphics.primitive.Patch'
K>> class(a(1).b(1))
ans =
'matlab.graphics.primitive.Patch'
Is this behaviour ok?.
By the way, are patches derived from handle?
Best regards,
Javier

답변 (1개)

Steven Lord
Steven Lord 2022년 2월 23일
As you've written your code, the patchs property stores a double array. You're in case Prop1 as described in the Property Default Values section on this documentation page. So when you try to assign the patch handle to that property using subscripted assignment, MATLAB must try to convert the patch handle to a double. It can do so because prior to the major update to the Handle Graphics system in release R2014b graphics object handles were stored as numbers rather than as objects. For backwards compatibility, the handle objects can be converted into numbers that MATLAB knows how to associate with the object to which the patch handle refers.
Instead of giving your property a default value of [], I recommend using gobjects to give it a default value that can store various different types of Handle Graphics handles. [Though you should be careful and read through the documentation about properties that contain objects first.]
g = gobjects(3, 1)
g =
3×1 GraphicsPlaceholder array: GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder
g(1) = figure;
g(2) = axes('Parent', g(1));
g(3) = plot(g(2), 1:10, 1:10)
g =
3×1 graphics array: Figure (1) Axes Line
We can look at the properties of any of those graphics objects by looking at the corresponding element of g.
g(2)
ans =
Axes with properties: XLim: [1 10] YLim: [1 10] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
Patch objects are handle objects just like line objects.
isa(g(3), 'handle')
ans = logical
1
ishandle(g(3))
ans = logical
1
ishghandle(g(3))
ans = logical
1

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by