i'm writing my first MATLAB O-O application and i'm confused about the implementation of compositions, aggregations and relationships in general.
My question is: how to implement an aggregation or an association one-to-many in matlab? Where i can take some examples?
Moreover, i'm using ArgoUml to design my application, is there any plugin to automatic code generation in matlab?
Thanks in advance
Here is a quick example of class association. The scenario consists of a course that can have many students enrolled:
Student.m
classdef Student < handle
properties
name
end
methods
function obj = Student(name)
if nargin > 0
obj.name = name;
end
end
function delete(obj)
fprintf('-- Student Destructor: %s\n',obj.name);
end
end
end
Course.m
classdef Course < handle
properties
name %# course name
std %# cell array of students
end
properties(Access = private)
lastIdx = 1;
end
methods
function obj = Course(name, capacity)
obj.name = name;
obj.std = cell(capacity,1);
end
function addStudent(obj, std)
if obj.lastIdx > numel(obj.std)
fprintf(2, 'Sorry, class is full\n');
return
end
obj.std{obj.lastIdx} = std;
obj.lastIdx = obj.lastIdx + 1;
end
function printClassRoster(obj)
fprintf('Course Name = %s\n', obj.name);
fprintf('Enrolled = %d, Capacity = %d\n', ...
obj.lastIdx-1, length(obj.std));
for i=1:obj.lastIdx-1
fprintf('ID = %d, Name = %s\n', i, obj.std{i}.name);
end
end
end
end
And here is a code to test the above classes:
c = Course('CS101', 3);
for i=1:4
name = sprintf('amro%d',i);
fprintf('Adding student: %s\n', name)
c.addStudent( Student(name) )
end
fprintf('\nClass Roster:\n=============\n')
c.printClassRoster()
fprintf('\nCleaning up:\n')
clear c
The output:
Adding student: amro1
Adding student: amro2
Adding student: amro3
Adding student: amro4
Sorry, class is full
-- Student Destructor: amro4
Class Roster:
=============
Course Name = CS101
Enrolled = 3, Capacity = 3
ID = 1, Name = amro1
ID = 2, Name = amro2
ID = 3, Name = amro3
Cleaning up:
-- Student Destructor: amro1
-- Student Destructor: amro2
-- Student Destructor: amro3
You may have a look at Object-Oriented Programming in MATLAB and in the documentation refer to Object-Oriented Programming.
I suggest to have a closer look Value or Handle Class — Which to Use
. To make it short, handle classes let you pass references around whereas value classes are always a copy of the original object.
I'd be surprised to find a plugin for ArgoUml, as MATLAB is mainly used by engineers and not software developers.
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. :)
There some problems with my OOP.
I have parent with clear table and child with same table.
When i'm trying to add object to table of child, object adds to parent's table.
Simple example:
Account = {}
Account.__index = Account
Account.kit = {}
function Account.create(balance)
local acnt = {} -- our new object
setmetatable(acnt,Account) -- make Account handle lookup
acnt.balance = balance -- initialize our object
return acnt
end
function Account:withdraw(amount)
self.balance = self.balance - amount
end
-- create and use an Account
acc = Account.create(1000)
acc:withdraw(100)
table.insert(acc.kit, "1")
print(#Account.kit)
print(#acc.kit)
Result is 1 and 1.
But must be 0 and 1.
How i can isolate child table from parent?
In Lua using acc.kit where acc is a table with the metatable Account will first search the key kit from the table acc and then from table Account.
In your code acc does not have anything with key kit, so the Account.kit will be accessed.
You can solve this simply by defining the kit table for the acc on creation
Account = {}
Account.__index = Account
Account.kit = {} -- You can now remove this if you do not use it - I preserved it to make the test prints to still work.
function Account.create(balance)
local acnt = {} -- our new object
setmetatable(acnt,Account) -- make Account handle lookup
acnt.balance = balance -- initialize our object
acnt.kit = {} -- define kit to be a subtable
return acnt
end
Example:
https://repl.it/B6P1
I'd recommend using closures to implement OOP:
local Animal = {}
function Animal.new(name)
local self = {}
self.name = name
function self.PrintName()
print("My name is " .. self.name)
end
return self
end
--now a class dog that will inherit from animal
local Dog = {}
function Dog.new(name)
-- to inherit from Animal, we create an instance of it
local self = Animal.new(name)
function self.Bark()
print(self.name .. " barked!")
end
return self
end
local fred = Dog.new("Fred")
fred.Bark()
fred.PrintName()
output:
Fred barked!
My name is Fred
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.