Output class object inherits the property/value pairs of a figure
조회 수: 8 (최근 30일)
이전 댓글 표시
I am trying to write a simple class. The purpose of the class is to create a figure. What I do is:
classdef myFig < handle
properties
Parent
end
methods
function obj = myFig
obj.Parent = figure;
end
end
end
My question is:
What should I do, in order any object that belongs to myFig class inherits the properties of the figure?
Ultimately, I would like to call a get method and view all the property/value pairs of the figure created.
Thank you
댓글 수: 0
채택된 답변
Sean de Wolski
2014년 12월 8일
Giorgos, rather than writing a figure class, write two classes for the vertical and horizontal scrollbars. Then add them to the figures as you need them. I have a bunch of utilities like this that I call my "Components"
For example:
figure
ColorPanel('Units','normalized','Position',[0 0 1 0.3], 'Color1',[1 0 0], 'Color2',[0 0 1])
The ColorPanel class is attached.
댓글 수: 0
추가 답변 (1개)
Guillaume
2014년 12월 8일
Assuming 2014b, as figure is not a class in earlier version:
For class A to inherit the properties of class B, class A has to derive from class B. Hence if you want to inherit the properties of the figure class (aka matlab.ui.Figure), myFig needs to derive from it:
classdef myFig < matlab.ui.Figure
Unfortunately for you, matlab.ui.Figure is sealed, which means you're not allowed to derive from it. So no inheritance for you.
What you'll have to do then, is hold the figure as a member variable and recreate all the properties of the figure:
classdef myFig < handle
properties (Access = private)
hfig
end
properties (Dependent)
Position;
%... and so on for all the properties of figure
end
methods
function this = myFig(hfig)
validateattributes(hfig, {'matlab.ui.Figure'}, {'scalar'});
this.hfig = hfig;
end
function varargout = get.Position(this)
varargout{:} = this.hfig.Position;
end
%... and so on for all the properties of figure
end
end
It's going to be tedious...
The last option is to override subsref in your class and implement some sort of dynamic properties. That is probably even more work and overriding subsref has some nasty side effects.
댓글 수: 3
Guillaume
2014년 12월 8일
편집: Guillaume
2014년 12월 8일
Actually, I didn't think that the dynamicprop class allowed you to create dependent dynamic properties, but it does. That makes it much easier to implement the myFig class:
classdef myFig < dynamicprops
properties (Access = private)
hfig
end
methods
function this = myFig(hfig)
validateattributes(hfig, {'matlab.ui.Figure'}, {'scalar'});
this.hfig = hfig;
%create all the figure properties as dependent dynamic properties:
for propname = properties(hfig)'
metaprop = addprop(this, propname{1});
metaprop.Dependent = true;
metaprop.SetMethod = @(this, varargin) SetDispatch(this, propname{1}, varargin{:});
metaprop.GetMethod = @(this) GetDispatch(this, propname{1});
end
end
end
methods (Access = private)
function SetDispatch(this, propname, varargin)
%called whenever a dependent dynamic property is set.
%just dispatch to the figure property
this.hfig.(propname) = varargin{:};
end
function varargout = GetDispatch(this, propname)
%called whenever a dependent dynamic property is read.
%just dispatch to the figure property
varargout{:} = this.hfig.(propname);
end
end
end
Still not as neat as inheriting from the class itself but not too bad.
Guillaume
2014년 12월 8일
Note that as written, it only dispatches to non-hidden properties of the figure class. If you wanted to also access the hidden properties (if any exists), you would have to use a meta.properties object on the class to get them.
At the same time, this would allow you to check for read-only properties.
Doing so is left as an exercise to the reader...
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!