MATLAB Answers

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

5 views (last 30 days)
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

Accepted Answer

Sean de Wolski
Sean de Wolski on 8 Dec 2014
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.

More Answers (1)

Guillaume
Guillaume on 8 Dec 2014
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 Comments
Guillaume
Guillaume on 8 Dec 2014
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...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!