Thread Subject:
create a new object instance based on a string (str2obj?)

Subject: create a new object instance based on a string (str2obj?)

From: Ben Ruppel

Date: 23 Aug, 2012 00:09:08

Message: 1 of 4

I am trying to make it easy to switch out certain behaviors of an object in Matlab (kind of the like the "Strategy" design pattern, but without bothering to do the interface part). What I want to do is assign a string to a variable. This string will match the name of a class definition file I have. I want to create a new object based on this class definition using the string variable contents.

So for instance:
string = 'behaviorA'
....
thisBehavior = string % <- I want this line of code to tell matlab to create a new object of type 'behaviorA'

%then my code can do whatever the object based on the class def does:
thisBehavior.whatIdo(....)

so the problem is that matlab doesn't take the hint that "thisBehavior = string"
wants it to create a new instance of the object with a name specified within 'string'. I tried surrounding "string" with parentheses (this is what I would do if I was trying to use a string to specify a component of a data structure) but this doesn't work.

Anyone have any ideas?

There is a str2func function that kind of does what I want, but for functions instead of objects. I think.

Thanks!

Subject: create a new object instance based on a string (str2obj?)

From: Phil Goddard

Date: 23 Aug, 2012 13:23:07

Message: 2 of 4

You will most likely have to resort to using feval or eval to call the appropriate constructor.
See the doc for those functions for more info.

Phil.

Subject: create a new object instance based on a string (str2obj?)

From: Steven_Lord

Date: 23 Aug, 2012 13:32:06

Message: 3 of 4



"Ben Ruppel" <brspam@cox.net> wrote in message
news:k13sb4$1oh$1@newscl01ah.mathworks.com...
> I am trying to make it easy to switch out certain behaviors of an object
> in Matlab (kind of the like the "Strategy" design pattern, but without
> bothering to do the interface part). What I want to do is assign a string
> to a variable. This string will match the name of a class definition file
> I have. I want to create a new object based on this class definition
> using the string variable contents.
> So for instance:
> string = 'behaviorA'
> ....
> thisBehavior = string % <- I want this line of code to tell matlab to
> create a new object of type 'behaviorA'
>
> %then my code can do whatever the object based on the class def does:
> thisBehavior.whatIdo(....)
>
> so the problem is that matlab doesn't take the hint that "thisBehavior =
> string"
> wants it to create a new instance of the object with a name specified
> within 'string'.

Yes. If it did, you wouldn't be able to define the variable string in the
first place, as that would need to be considered a request to construct a
behaviorA for consistency.

> I tried surrounding "string" with parentheses (this is what I would do if
> I was trying to use a string to specify a component of a data structure)
> but this doesn't work.
>
> Anyone have any ideas?
>
> There is a str2func function that kind of does what I want, but for
> functions instead of objects. I think.

There are several ways to do what you're looking for, and the one I'm going
to recommend is to step away from specifying the class name as a string.
Instead specify it as a function handle to the constructor of the object:

makeANew = @behaviorA;
thisBehavior = makeANew(requiredInputsForConstructorOfbehaviorA);

If the constructor does not require any inputs, you will still need to use
the parentheses.

thisBehavior = makeANew();

Slightly less recommended is to convert the string into a function handle:

string = 'behaviorA';
makeANew = str2func(string);
thisBehavior = makeANew(requiredInputsForConstructorOfbehaviorA);

The reason I recommend skipping the string step entirely is that it will
avoid this problem if you plan to create a standalone application from your
code:

http://www.mathworks.com/help/toolbox/compiler/br2cqa0-2.html#br2cqa0-5

The explicit function handle tells MATLAB that it will need the behaviorA
function/constructor. The string + str2func doesn't make that explicit.

If you only care about a small number of different potential classes, you
could use a factory function.

function obj = buildMeA(classname, varargin)
switch classname
    case 'behaviorA'
        obj = behaviorA(varargin{:});
    case 'behaviorB'
        obj = behaviorB(varargin{:});
    otherwise
        error('I don''t know what to make.');
end

The explicit calls to the constructors inside buildMeA avoid the standalone
application issue.

There's at least one other way to do this, but I don't recommend it on
general principles.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: create a new object instance based on a string (str2obj?)

From: Ben Ruppel

Date: 23 Aug, 2012 14:06:07

Message: 4 of 4

"Steven_Lord" <slord@mathworks.com> wrote in message <k15bcl$6vb$1@newscl01ah.mathworks.com>...
>
>
> "Ben Ruppel" <brspam@cox.net> wrote in message
> news:k13sb4$1oh$1@newscl01ah.mathworks.com...
> > I am trying to make it easy to switch out certain behaviors of an object
> > in Matlab (kind of the like the "Strategy" design pattern, but without
> > bothering to do the interface part). What I want to do is assign a string
> > to a variable. This string will match the name of a class definition file
> > I have. I want to create a new object based on this class definition
> > using the string variable contents.
> > So for instance:
> > string = 'behaviorA'
> > ....
> > thisBehavior = string % <- I want this line of code to tell matlab to
> > create a new object of type 'behaviorA'
> >
> > %then my code can do whatever the object based on the class def does:
> > thisBehavior.whatIdo(....)
> >
> > so the problem is that matlab doesn't take the hint that "thisBehavior =
> > string"
> > wants it to create a new instance of the object with a name specified
> > within 'string'.
>
> Yes. If it did, you wouldn't be able to define the variable string in the
> first place, as that would need to be considered a request to construct a
> behaviorA for consistency.
>
> > I tried surrounding "string" with parentheses (this is what I would do if
> > I was trying to use a string to specify a component of a data structure)
> > but this doesn't work.
> >
> > Anyone have any ideas?
> >
> > There is a str2func function that kind of does what I want, but for
> > functions instead of objects. I think.
>
> There are several ways to do what you're looking for, and the one I'm going
> to recommend is to step away from specifying the class name as a string.
> Instead specify it as a function handle to the constructor of the object:
>
> makeANew = @behaviorA;
> thisBehavior = makeANew(requiredInputsForConstructorOfbehaviorA);
>
> If the constructor does not require any inputs, you will still need to use
> the parentheses.
>
> thisBehavior = makeANew();
>
> Slightly less recommended is to convert the string into a function handle:
>
> string = 'behaviorA';
> makeANew = str2func(string);
> thisBehavior = makeANew(requiredInputsForConstructorOfbehaviorA);
>
> The reason I recommend skipping the string step entirely is that it will
> avoid this problem if you plan to create a standalone application from your
> code:
>
> http://www.mathworks.com/help/toolbox/compiler/br2cqa0-2.html#br2cqa0-5
>
> The explicit function handle tells MATLAB that it will need the behaviorA
> function/constructor. The string + str2func doesn't make that explicit.
>
> If you only care about a small number of different potential classes, you
> could use a factory function.
>
> function obj = buildMeA(classname, varargin)
> switch classname
> case 'behaviorA'
> obj = behaviorA(varargin{:});
> case 'behaviorB'
> obj = behaviorB(varargin{:});
> otherwise
> error('I don''t know what to make.');
> end
>
> The explicit calls to the constructors inside buildMeA avoid the standalone
> application issue.
>
> There's at least one other way to do this, but I don't recommend it on
> general principles.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thank you very much Steve! I will give your recommended approach a shot. The factory function also occurred to me, but I was looking for a way to do this without having to edit a case statement. Mostly it is an exercise in seeing what I can do with the language :) Thanks again!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
oop dynamic Ben Ruppel 22 Aug, 2012 20:14:10
rssFeed for this Thread

Contact us