I'd like to customize some of the MATLAB uicontrols (such as the drop-down box) to give them more user-friendly functionality.
My question is: Is it possible to extend/inherit the uicontrol? If so, how do you do it? If not, is there a workaround?
I have tried this basic code just to get it setup, but I receive the following error:
The specified super-class 'uicontrol' contains a parse error or cannot be found on
MATLAB's search path, possibly shadowed by another file with the same name.
classdef ComboBox < uicontrol
methods(Access = public)
function obj = ComboBox()
set(obj, 'Style', 'popup');
end
end
end
The error occurs when I try to add it to a figure:
cmb = ComboBox();
set(cmb, 'Parent', obj.ui_figure);
Edit: After thinking about it, I think this would be a decent workaround, however, I'd still like to know how to extend uicontrol if it's possible.
classdef ComboBox < uicontrol
properties(Access = public)
Control;
end
methods(Access = public)
function obj = ComboBox(parent, items)
obj.Control = uicontrol();
set(obj.Control, 'Style', 'popup');
set(obj.Control, 'Parent', parent);
set(obj.Control, 'String', items);
end
end
end
There is no documented way (as of R2013a) to write subclasses to MATLAB Handle Graphics
classes. In fact, because of the way MATLAB stores the hg objects, we can't even get
the class of an hg object easily. For example:
>> fig = figure();
>> class(f)
ans =
double
>> isa(f, 'figure')
ans =
0
>> ishghandle(f)
ans =
1
One solution to this is to write a class which subclasses either handle, or
hgsetget, and keeps a handle to a uicontrol object as a private property.
For example:
classdef ComboBox < hgsetget
properties (Access = private)
Control
end
properties
% extend the functionality of your new uicontrol with additional
% properties
newProp1
newProp2
end
properties (Dependent = true)
% make any properties of uicontrol for which you want to still
% allow access as dependent properties. define set and get methods
% for these properties below
fontSize
foregroundColor
backgroundColor
items
end
methods
function obj = ComboBox(parent, items)
obj.Control = uicontrol(parent, 'Style', 'popup', ...
'String', items);
% make sure to delete this object if you delete the uicontrol
set(obj.Control, 'DeleteFcn', {#(source, eventData)delete(obj)})
end
% Define set and get methods for your new properties. These methods
% will set the actual properties, and then modify the uicontrol in
% some way
function prop = get.newProp1(obj)
prop = obj.newProp1;
end
function set.newProp1(obj, newVal)
obj.newProp1 = newVal;
% do something else
end
function prop = get.newProp2(obj)
prop = obj.newProp2;
end
function set.newProp2(obj, newVal)
obj.newProp2 = newVal;
% do something else
end
% Define set and get methods for any uicontrol properties you wish
% to retain. These methods will simply redirect calls to the
% uicontrol object.
function size = get.fontSize(obj)
size = get(obj.Control, 'FontSize');
end
function set.fontSize(obj, newSize)
set(obj.Control, 'FontSize', newSize);
end
function color = get.backgroundColor(obj)
color = get(obj.Control, 'BackgroundColor');
end
function set.backgroundColor(obj, newColor)
set(obj.Control, 'BackgroundColor', newColor);
end
function color = get.foregroundColor(obj)
color = get(obj.Control, 'ForegroundColor');
end
function set.foregroundColor(obj, newColor)
set(obj.Control, 'ForegroundColor', newColor);
end
% You can even rename some uicontrol properties to fit your
% purpose.
function items = get.items(obj)
items = get(obj.Control, 'String');
end
function set.items(obj, newItems)
set(obj.Control, 'String', newItems);
end
end
end
You can then use the ComboBox as you would any other uicontrol handle:
obj = ComboBox(figure, 'hello|goodbye');
set(obj, 'items', 'newItem1|newItem2');
There are undocumented ways of extending handle graphics classes,
but I'm not familiar with them. Check out this reference:
http://undocumentedmatlab.com/blog/introduction-to-udd/
Related
I am building a game with enemies and players, all of which are set up to have various states for animation and behavior. The parent class for both is Entity.lua, from which they inherit variables and methods. However, while both enemies and players are inheriting the variables, for some reason the enemies do not inherit the methods. So, if I try to call snakey:changeState('search'), for example, it gives me an error message "Attempt to call method 'changeState' (a nil value)".
I have used the same sequence for creating entities in several games in the past and never had this problem. In fact, if I create the Player in the same way, file and location as the enemies, I receive no error messages.
Here is the code where I create the entities.
local snakey
snakey = Snakey {
platform = platform,
player1 = self.player1,
player2 = self.player2,
stateMachine = StateMachine {
['search'] = function() return SnakeySearchState(snakey) end,
['chasing'] = function() return SnakeyChasingState(snakey) end,
['idle'] = function() return SnakeyIdleState(snakey) end
}
}
-- snakey:changeState('search')
-- snakey.stateMachine:change('search', params)
table.insert(self.entities, snakey)
The two coded out lines are where I noticed the problem. The first line gives and error and the second does work, but is not satisfactory because it is a work-around.
Here is the code for Entity.lua: I don't include details of the functions for brevity, but all are working properly for when player calls them.
Entity = Class{}
function Entity:init(def)
-- position
self.x = def.x
self.y = def.y
self.gravity = 6
-- many more variables
end
function Entity:changeState(state, params)
self.stateMachine:change(state)
end
function Entity:update(dt)
self.stateMachine:update(dt)
end
function Entity:collides(entity)
-- do something
end
function Entity:onDamage()
-- do something
end
function Entity:render()
- renders sprite
end
Player code (in brief)
Player = Class{__includes = Entity}
function Player:init(def)
Entity.init(self, def)
-- more variables
end
function Player:update(dt)
Entity.update(self, dt)
end
function Player:render()
Entity.render(self)
end
And perhaps the trouble spot, one one enemy's script
Snakey = Class{__includes = Entity}
function Snakey:init(def)
Entity.init(self, def)
-- yet more variables
end
function Snakey:update(dt)
Entity.update(self, dt)
-- entity behavior (works fine, so omitted)
end
function Snakey:render()
Entity.render(self)
end
Thank you very much for your help. I'm feeling quite frustrated because this sequence has worked in the past and I would really like to know why it's not calling those Entity methods.
Adding the class library
--Copyright (c) 2010-2013 Matthias Richter
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
It turns out that sequence matters. In trying to create the minimum reproducible code I could not reproduce the error. After some searching (and a little frustration), I noticed that in Dependencies.lua I was requiring the enemies BEFORE Entity.lua, but Player.lua was required after. I would have thought this wouldn't matter, since everything was imported into the program on frame 1 and I was creating entities on something like frame 1000, but alas. Anyway, problem solved! Always require parent classes before child classes... Lesson learned. :)
I'm learning lua and i'm trying the OOP approach.
To start, I'm trying to make a grid class, but I think i'm still lacking some knowledge to make it do what I want it to do.
This is my code so far:
local screen = require( 'data.screen')
Grid = {}
Grid_mt = { __index = Grid }
--- Constructs a new Grid object.
function Grid:new( params )
local self = {}
local function try( self, event )
print(self.empty)
end
for i = 1, screen.tilesAcross do
if not self[i] then
self[i] = {};
end
for j = 1, screen.tilesDown do
self[i][j] = display.newImageRect( params.group, "images/playable.png", screen.tileWidth, screen.tileHeight )
self[i][j].x = (j - 1) * (screen.tileWidth + screen.tileSpacing) + screen.leftSpacing
self[i][j].y = (i - 1) * (screen.tileHeight + screen.tileSpacing) + screen.topSpacing
self[i][j]._x = i
self[i][j]._y = j
self[i][j].enable = false
self[i][j].empty = true
self[i][j].tap = try
self[i][j]:addEventListener( "tap", self[i][j] )
end
end
setmetatable( self, Grid_mt )
return self
end
function Grid:setEnable(value, x, y)
if value ~= true and value ~= false then
error("Boolean expected")
end
self[x][y].enable = value
end
function Grid:getEnable(x, y)
return self[x][y].enable
end
function Grid:setEmpty(value, x, y)
if value ~= true and value ~= false then
error("Boolean expected")
end
self[x][y].empty = value
end
function Grid:getEmpty(x, y)
return self[x][y].empty
end
function Grid:SetmColor()
self[1][4]:setFillColor( 255,255 )
end
I have 2 questions:
My event works but I would like to do something like: print(self:getEmpty() )
but whenever I try to use a method in my event, it doesn't work "attempt to call method 'getEmpty' (a nil value)"
and also the setfillcolor wwon't work, I want to be able to change the color of a case with it.
Thanks for your time!
and if i'm going to the wrong road, let me know, and by the way, I have a working code without make a class, this is just for training purpose :)
Thnaks!
The issue is that self[i][j] is not a Grid, only self is a Grid. So when the event handler is called, self in the handler is the display object, not the Grid object. If the handler needs to know the grid object, you could assign the grid to each display object:
self[i][j].grid = self
then in try you could do
grid = self.grid
But in this case you may get a cleaner design if you have the tap handler not be specific to each display object. In this case you would use an upvalue, you can just use self:
function Grid:new( params )
local self = {}
local function try( event ) -- omit the first param, self is an upvalue
print(self.empty)
end
for i = 1, screen.tilesAcross do
...
for j = 1, screen.tilesDown do
self[i][j] = display.newImageRect( ... )
...
self[i][j]:addEventListener( "tap", try )
end
end
Since listener given is a function rather than a table, it gets only one parameter, the event, and self is an upvalue so it will be the Grid instance created just above the try function.
For the first question, more information would be needed. See my comment above.
As for the setFillColor not working, the following is taken from the Corona docs:
object:setFillColor( gray )
object:setFillColor( gray, alpha )
object:setFillColor( red, green, blue )
object:setFillColor( red, green, blue, alpha )
object:setFillColor( gradient )
gray, red, green, blue, alpha (optional)
Numbers between 0 and 1 that represent the corresponding
value for that channel. alpha represents the opacity of the object.
Now, when you are trying to execute:
self[1][4]:setFillColor( 255,255 )
you are passing the values for gray and alpha channels. The values NEED TO BE less than, or equal to 1. Probably you want to pass 1 (as 255 is generally the max. value)
I need to build an array of objects of class ID using arrayfun:
% ID.m
classdef ID < handle
properties
id
end
methods
function obj = ID(id)
obj.id = id;
end
end
end
But get an error:
>> ids = 1:5;
>> s = arrayfun(#(id) ID(id), ids)
??? Error using ==> arrayfun
ID output type is not currently implemented.
I can build it alternatively in a loop:
s = [];
for k = 1 : length(ids)
s = cat(1, s, ID(ids(k)));
end
but what is wrong with this usage of arrayfun?
Edit (clarification of the question): The question is not how to workaround the problem (there are several solutions), but why the simple syntax s = arrayfun(#(id) ID(id), ids); doesn't work. Thanks.
Perhaps the easiest is to use cellfun, or force arrayfun to return a cell array by setting the 'UniformOutput' option. Then you can convert this cell array to an array of obects (same as using cat above).
s = arrayfun(#(x) ID(x), ids, 'UniformOutput', false);
s = [s{:}];
You are asking arrayfun to do something it isn't built to do.
The output from arrayfun must be:
scalar values (numeric, logical, character, or structure) or cell
arrays.
Objects don't count as any of the scalar types, which is why the "workarounds" all involve using a cell array as the output. One thing to try is using cell2mat to convert the output to your desired form; it can be done in one line. (I haven't tested it though.)
s = cell2mat(arrayfun(#(id) ID(id), ids,'UniformOutput',false));
This is how I would create an array of objects:
s = ID.empty(0,5);
for i=5:-1:1
s(i) = ID(i);
end
It is always a good idea to provide a "default constructor" with no arguments, or at least use default values:
classdef ID < handle
properties
id
end
methods
function obj = ID(id)
if nargin<1, id = 0; end
obj.id = id;
end
end
end
I'm trying to list classes I created in some folder in my Matlab folder - using only their name (class name)
as an example, I have a class called 'SimpleString' - and I'm aiming to instantiate an object from that class, if all I know is that its name is 'SimpleString'
So in realtime, I'd like to find out what classes are in a folder (done), then be able to instantiate any of those classes (my question)
Thanks
Use a package to access class constructors with .()-notation.
A Matlab package is simply a folder/directory with a name that begins with +:
+mypackage/foo.m:
classdef foo
methods
function obj = foo(arg1, arg2)
%foo constructor
end
end
end
With class foo defined this way, you can access the constructor of mypackage.foo as
class_name = 'foo';
o = mypackage.(class_name)('arg1_value', 'arg2_value');
Use str2func to get a function handle to the constructor. You can then call it with whatever arguments are appropriate.
>> m = str2func('containers.Map')
m =
#containers.Map
>> x = m({'foo', 'bar'}, {0, 1})
x =
containers.Map handle
Package: containers
Properties:
Count: 2
KeyType: 'char'
ValueType: 'double'
Methods, Events, Superclasses
You can use the WHAT function to discover classes (as well as functions,packages,etc...) in a certain folder, then call METHODS to find the signature of the constructor of the class (some parsing needed here), finally using FEVAL (passing arguments if any) to create an object from this class.
You could also use meta.class to get all sorts of meta-information about your classes.
EDIT
Here is some code to illustrate what I had in mind:
%# folder containing your classes
pathName = fullfile(pwd,'folder');
%# make sure it is on the path
p = textscan(path, '%s', 'Delimiter',';'); p=p{1};
if ~any(ismember(p,pathName))
addpath(pathName)
end
%# list MATLAB files
w = what(pathName);
%# get class names
fNames = cellfun(#(s) s(1:end-2), w.m, 'Uni',false); %# remove .m extension
fNames = [fNames ; w.classes]; %# add classes in #-folders
%# get classes metadata
mt = cellfun(#meta.class.fromName, fNames, 'Uni',false); %# get meta-data
mt = mt( ~cellfun(#isempty,mt) ); %# get rid of plain functions
%# build object from each class
objects = cell(numel(mt),1);
for i=1:numel(mt)
%# get contructor function
ctorMT = findobj(mt{i}.MethodList, 'Access','public', 'Name',mt{i}.Name);
%# get number of contructor arguments
numArgs = numel(ctorMT.InputNames);
%# create list of arguments (using just zeros)
args = repmat({0}, [numArgs 1]);
%# create object
try
obj = feval(ctorMT.Name,args{:});
catch ME
warning(ME.identifier, ME.message)
obj = [];
end
%# store object
objects{i} = obj;
end
As you can see, I found it easier to simply use meta.class to get metadata about the classes, instead of manually parsing the output of methods('fcn','-full') as I originally suggested.
However this is not perfect, as there is no way to find out what type of input each constructor expect (only how many). I opted to always pass 0 for each argument..
To test the implementation above, I create these sample classes (one in a self-contained file, the other defined in #-folder with multiple files):
folder/hello.m
classdef hello
properties
name = '';
end
methods
function this = hello()
this.name = 'world';
end
function val = get.name(obj)
val = obj.name;
end
function obj = set.name(obj,val)
obj.name = val;
end
function say(obj)
fprintf('Hello %s!\n',obj.name);
end
end
end
folder/#hello2/hello2.m
classdef hello2
properties
name
end
methods
function this = hello2(val)
this.name = val;
end
function val = get.name(obj)
val = obj.name;
end
function obj = set.name(obj,val)
obj.name = val;
end
end
methods
say(obj)
end
end
folder/#hello2/say.m
function say(obj)
fprintf('Hello2 %s!\n', obj.name);
end
You can use eval to instantiate the class using just the class name.
instance = eval('SimpleString');
However, if you're simply iterating through all the m-files in a folder containing class definitions and grabbing the file names, you'll only be able to invoke the default constructor using this method.
I have a class like so:
classdef Vehicle < handle
%Vehicle
% Vehicle superclass
properties
Is_Active % Does the vehicle exist in the simualtion world?
Speed % [Km/Hour]
end
methods
function this = Vehicle(varargin)
this.Speed = varargin{1}; % The speed of the car
this.Is_Active = true;
end
end
end
I create my Vehicle-class objects in a cell form (don't ask me why - it's a laymen's workaround for global setting):
Vehicles{1} = Vehicle(100);
Vehicles{2} = Vehicle(200);
Vehicles{3} = Vehicle(50);
Vehicles{1}.Is_Active = true;
Vehicles{2}.Is_Active = true;
Vehicles{3}.Is_Active = true;
My questions:
1. Is there a way to set all three objects' active in a single command?
2. Is there a way to get all three objects' Speed in a single command?
3. Is there a way to query which vehicles are faster than X in a single command?
Thanks
Gabriel
For the members of the same class you can use round brackets (regular array):
Vehicles(1) = Vehicle(100);
Vehicles(2) = Vehicle(200);
Vehicles(3) = Vehicle(50);
To set all objects use deal:
[Vehicles(:).Is_Active] = deal( true );
You could also initialize an array of objects in the first place.
For your questions (2) and (3) the syntax is equivalent to those of MATLAB structures:
speedArray = [Vehicles.Speed];
fasterThanX = Vehicles( speedArray > X );
Such vectorization notation is a strong point of MATLAB and is used extensively.