I'd like to iterate over Matlab classes. Is this possible?
My application has several subclasses based on different structures and algorithms, but each subclass implements the same (class and instance) methods. So it is natural for me to have test-cases which have the subclass as a parameter. In Ruby, for example, this is would be easy
[ClassA, ClassB].each do |cls|
if cls.some_class_method? then
instance = cls.new
:
end
end
but I don't seem to be able to find a way to do this in Matlab, there don't seem to be "class handles".
Is there a way?
[edit]
Following #zeeMonkeez's solution I ended up with the following
function varargout = call_method(class_name, method_name, varargin)
qualified_name = sprintf('%s.%s', class_name, method_name);
[varargout{1:nargout}] = feval(qualified_name, varargin{:});
end
which I can call in my test-case like
class_name = 'MVPolyCube';
:
[x, y, z] = call_method(class_name, 'variables');
which solves my problem and DRYs up my test suite. Thanks all!
If you don't mind using feval, you could do something like the code below.
Basically, we test if a static method of a given name exists for a class name (using meta.class). Then we get the return value of said method using feval. Based on that, we instantiate an object or not (again using feval).
A.m:
classdef A
methods
function whoami(this)
fprintf('I am an A\n');
end
end
methods(Static)
function out = a
out = true;
fprintf('A.a returns %i\n', out);
end
end
end
B.m:
classdef B
methods
function whoami(this)
fprintf('I am a B\n');
end
end
methods(Static)
function out = a
out = false;
fprintf('B.a returns %i\n', out);
end
function out = b
out = true;
fprintf('B.b returns %i\n', out);
end
end
end
has_static_method.m, will be used to test if a class has a static function:
function res = has_static_method(class_name, method_name)
mc = meta.class.fromName(class_name);
ml = mc.MethodList;
ml = ml([mc.MethodList.Static]);
res = any(strcmp(method_name, {ml.Name}));
end
test.m:
classes = {'A', 'B'};
for i_c = 1:numel(classes)
klass_name = classes{i_c};
static_function_name = 'a';
if has_static_method(klass_name, static_function_name) && feval(sprintf('%s.%s', klass_name, static_function_name))
an_object = feval(klass_name);
an_object.whoami
end
static_function_name = 'b';
if has_static_method(klass_name, static_function_name) && feval(sprintf('%s.%s', klass_name, static_function_name))
an_object = feval(klass_name);
an_object.whoami
end
end
Related
As the title says I am setting a property with the constructor and would like to access the property later as a get function that is static. How would I do this in MATLAB?
classdef Wrapper
properties(Access=public)
dataStruct
end
methods
function data = Wrapper(filePath)
if nargin == 1
data.dataStruct=load(filePath)
end
end
end
methods(Static)
function platPosition = getPlatPosition()
platPosition = dataStruct.someField
end
end
end
--------------------------
import pkg.Wrapper.*
test = Wrapper('sim.mat')
pos = test.getPlatPosition
As far as I know MATLAB doesn't have static properties like other OOP languages [ref]. Only static properties can be used inside the static methods. The closest you can get in MATLAB classes to static property is Constant property. The downside is the constant property has to be initialized and is read-only. Inside the static method, You can access read-only/constant property with class name.
classdef Wrapper
properties(Constant=true)
dataStruct=load('\path\to\sim.mat');
end
methods
function data = Wrapper()
%do something with object
end
end
methods(Static=true)
function platPosition = getPlatPosition()
platPosition = Wrapper.dataStruct.Fieldname;
end
end
end
In your case, you could accept object as an input argument of your static method.
classdef Wrapper
properties(Access=public)
dataStruct
end
methods
function data = Wrapper(filePath)
if nargin == 1
data.dataStruct=load(filePath)
end
end
end
methods(Static)
function platPosition = getPlatPosition(obj)
platPosition = obj.dataStruct.someField
end
end
end
--------------------------
import pkg.Wrapper.*
test = Wrapper('sim.mat')
pos = test.getPlatPosition(test);
I work within a project which has several classes which define properties that use essentially the same set method. To make the code more readable, I want to implement a commonSetter method. The overall goal is to include this commonSetter method in the superclass, so that all the classes could use it.
The question was already posted here, but unfortunately, the answer is not working. I changed the code to the following, but get the error: Maximum recursion limit of 500 reached.
classdef MyClass
properties
A
B
end
methods
function mc = MyClass(a,b) % Constructor
mc.A = a;
mc.B = b;
end
function mc = set.A(mc, value) % setter for A
mc = mc.commonSetter(value, 'A');
end
function mc = set.B(mc, value) % setter for B
mc = mc.commonSetter(value, 'B');
end
end
methods(Access = protected)
function mc = commonSetter(mc, value, property)
% do some stuff
disp('Made it into the commonSetter!')
mc.(property) = value;
end
end
end
So far I know that there is an infinite loop where mc.(property) = value; calls set.A (or set.B), which in turn calls commonSetter.
In my post # MathWorks the following was suggested:
To break that loop I guess you should look at builtin() and subsasgn(). Maybe Overriding subsref and subsasgn - effect on private properties can be of some help.
Currently, I have troubles realizing the suggestions and additionally not very comfortable to overwrite subsasgn() as I'm not sure how it will affect the overall project. I would like to know, if someone has other ideas or knows how to overwrite subsasgn() safely.
To solve the recursion error, you could just let the commonSetter method output the new value instead of the object.
classdef MyClass
properties
A
B
end
methods
function mc = MyClass(a, b)% Constructor
mc.A = a;
mc.B = b;
end
function mc = set.A(mc, value)% setter for A
mc.A = mc.commonSetter(value, 'A'); % update mc.A
end
function mc = set.B(mc, value)% setter for B
mc.B = mc.commonSetter(value, 'B');
end
end
methods (Access = protected)
function new_value = commonSetter(mc, value, property) % only return the new value
% do some stuff
disp('Made it into the commonSetter!')
if value > 5
new_value = -10;
else
new_value = value;
end
end
end
end
I am starting with oop in Matlab and seem to miss something.
classdef car < handle
properties (Access = public)
a
b
end
methods
function obj = update(obj)
obj.b = updateB(obj.a, obj.b);
end
function B = updateB(a, b)
B = a + b;
end
end
end
I get the famous Undefined function 'updateB' for input arguments of type 'double'. error every time, I try to call the function update. Oddly, it works if I change updateB to:
function B = updateB(obj)
B = obj.a + obj.b;
end
What am I missing? I do not always want to call updateB with obj, because I want to use the function without using the actual object's properties.
When, if you don't want the method to rely on a specific instance, use this approach:
methods
function obj = update(obj)
obj.b = car.updateB(obj.a, obj.b);
end
end
methods (Static)
function B = updateB(a, b)
B = a + b;
end
end
I have a class that encapsulates access to an array in a wierd way;
The class constructor takes a function handle which is some kind of transformation of indexes before passing them to the array
classdef MyClass
properties
arr
accessHandle
end
methods
function obj = MyClass(array, trans)
obj.arr = array;
obj.accessHandle = #(i) obj.arr(trans(i))
end
end
The problem is the anonymous function copies the array into it's own workspace and if we change the array, it doesn't change in the function.
Essentially what is needed is to pass to the anonymous function the 'this' pointer/reference like in Java/C++/etc
The simple solution is to create a handle to the array and pass it along to the function:
classdef MyClass
properties
arr
accessHandle
end
methods
function obj = MyClass(array, trans)
obj.arr = array;
tmp = PropertyReference(obj, 'arr'); %See http://stackoverflow.com/questions/7085588/matlab-create-reference-handle-to-variable
%for the definition
obj.accessHandle = #(i) tmp(trans(i));
end
end
end
The problem now is when I pass an instance of the class to a function, the reference passed still refers to the object outside the function:
function foo(ins)
ins.arr = [1 2];
disp(ins.accessHandle(1));
end
cl = MyClass([0 3], #(x) x);
foo(cl) //output 0 instead of 1
disp(ins.accessHandle(1)) //output 0
EDIT: The class should be a value class, the semantics are that when a copy of the class is made, the accessHandle field changes the array handle it uses.
How can I achieve the right semantics ?
Currently, your class is a value class (MATLAB's default). If you want to be able to pass objects around by reference (changes will be reflected in the original object), you'll to make it a handle class by subclassing handle
classdef MyClass < handle
properties
arr
accessHandle
end
methods
function obj = MyClass(array, trans)
obj.arr = array;
obj.accessHandle = #(i) obj.arr(trans(i))
end
end
end
And then you can use it the way you expect
m = MyClass([1 2 3], #(x)x);
m.accessHandle(2)
% 2
m.arr(2) = 5;
m.accessHandle(2)
% 5
More information about the difference between the two can be found here.
Edit
If you need MyClass to be a value class, then you could store the trans value as a property and then have a normal method to access the value
classdef MyClass
properties
arr
trans
end
methods
function obj = MyClass(array, trans)
obj.arr = array;
obj.trans = trans;
end
function res = access(obj, k)
res = obj.arr(obj.trans(k));
end
end
end
Or if you want, you could make accessHandle a dependent property that returns a function handle.
classdef MyClass
properties
arr
trans
end
properties (Dependent)
accessHandle
end
methods
function obj = MyClass(array, trans)
obj.arr = array;
obj.trans = trans;
end
function res = get.accessHandle(obj)
res = #(i)obj.arr(obj.trans(i));
end
end
end
As I do not seem to be able to edit my old question (Matlab - Function taking no arguments but not static), here it is again:
I am trying to implement the following:
classdef asset
properties
name
values
end
methods
function AS = asset(name, values)
AS.name = name;
AS.values = values;
end
function out = somefunction1
ret = somefunction2(asset.values);
out = mean(ret);
return
end
function rets = somefunction2(vals)
n = length(vals);
rets = zeros(1,n-1);
for i=1:(n-1)
rets(i) = vals(i)/vals(i+1);
end
return
end
end
end
But I am getting the error that somefunction1 should be static. But if it's static then it can't access the properties anymore. How would I resolve this issue?
Basically I want to be able to write something like this:
AS = asset('testname',[1 2 3 4 5]);
output = AS.somefunction1();
as opposed to writing
AS = asset('testname',[1 2 3 4 5]);
output = AS.somefunction1(AS);
For accessing the properties of an object in a method, you need to pass that object as argument to the method. If you don't need a specific object in order to perform a function, then make it static (belongs to the class, but does not operate on a specific object).
So, compare the original code:
methods
% ...
function out = somefunction1
ret = somefunction2(asset.values);
out = mean(ret);
return
end;
function rets = somefunction2(vals)
n = length(vals);
rets = zeros(1,n-1);
for i=1:(n-1)
rets(i) = vals(i)/vals(i+1);
end
return
end
end
with the correct code:
methods
% ...
% this function needs an object to get the data from,
% so it's not static, and has the object as parameter.
function out = somefunction1(obj)
ret = asset.somefunction2(obj.values);
out = mean(ret);
end;
end;
methods(Static)
% this function doesn't depend on a specific object,
% so it's static.
function rets = somefunction2(vals)
n = length(vals);
rets = zeros(1,n-1);
for i=1:(n-1)
rets(i) = vals(i)/vals(i+1);
end;
end;
end;
To call the method, you'd write indeed (please test):
AS = asset('testname',[1 2 3 4 5]);
output = AS.somefunction1();
because, in MATLAB, this is 99.99% of cases equivalent to:
AS = asset('testname',[1 2 3 4 5]);
output = somefunction1(AS);
The differences appear when you're overriding subsref for the class, or when the object passed to the method is not the first in the argument list (but these are cases that you should not concern with for now, until you clarify the MATLAB class semantics).