Output class object inherits the property/value pairs of a figure

3 次查看(过去 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

采纳的回答

Sean de Wolski
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.

更多回答(1 个)

Guillaume
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
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
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 CenterFile 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!

Translated by