Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Static class call via string variable

Asked by Jim Hokanson on 27 Apr 2013

Is it possible to call a static method or Constant property from a class whose name is represented by a string without using eval?

my_class_string = 'my_class'
eval([my_class_string '.MY_CONSTANT_PROPERTY'])

1 Comment

Daniel on 28 Apr 2013

At the point in which it is a string it might, and probably is, too late. How do you end up with a string and not an object?

Jim Hokanson

Products

No products are associated with this question.

3 Answers

Answer by Matt J on 28 Apr 2013
Edited by Matt J on 28 Apr 2013
Accepted answer

Here's a way to access a constant property without creating an object of the class.

    mc=meta.class.fromName('myclass');
    mp=mc.PropertyList;
     [~,loc]=ismember('MY_CONSTANT_PROPERTY',{mp.Name});
    propertyValue = mp(loc).DefaultValue;

For a static method, I don't know why you wouldn't use EVAL, but note that you could use FEVAL as well, if it feels better,

    out = feval('myclass.MY_STATIC_METHOD');

2 Comments

Jim Hokanson on 28 Apr 2013

The property solution is awful, but I suspect it is probably the only way to accomplish what I was looking for without using eval. I'll probably keep using eval for now. Thanks!

The thought with avoiding eval in general is that I tend to think eval makes it more difficult to discern what is going on. I also feel like eval slows down the optimization just a bit (not tested).

Matt J on 28 Apr 2013

The property solution is awful, but I suspect it is probably the only way to accomplish what I was looking for

Awful because of the code complexity? I wouldn't agree with that as a criticism. You can always hide the lines of code inside an mfile function of your own making.

However, I tend to agree with Daniel that your situation is already awful, needing to work with dynamic classnames and not objects... or at least "awfully peculiar" ;)

I also feel like eval slows down the optimization just a bit (not tested)

No need to test it. It's well known...

Matt J
Answer by per isakson on 27 Apr 2013
Edited by per isakson on 27 Apr 2013

It's without eval

    foo = str2func( my_class_string );
    obj = foo();
    obj.MY_CONSTANT_PROPERTY
    ans =
        17

where

    classdef  my_class 
        properties ( Constant = true )
            MY_CONSTANT_PROPERTY = 17
        end
    end

An alternative

    obj = feval( 'my_class' )
    obj.MY_CONSTANT_PROPERTY

But, is it any better than eval? And it does neither depend on static nor constant.

0 Comments

per isakson
Answer by Cedric Wannaz on 27 Apr 2013
Edited by Cedric Wannaz on 27 Apr 2013

Yes, you can proceed the same way as with dynamic field names:

 myObject = myClass() ;
 methodName = 'foo' ;
 x = myObject.(methodName)(arg1, arg2) ;

1 Comment

Jim Hokanson on 27 Apr 2013

The question is when the class name is dynamic, not the method or property. Thanks though.

Cedric Wannaz

Contact us