Can I call a classes method using str2func?

752 Views Asked by At

I am trying to use str2func to call different methods of my class depending on a specific properties value (in this case obj.type).

So, I have

classdef myClass
    properties
           type %# values are different file extensions (LSM, TIFF, OIF etc...)
    end

    methods(public)
          function process(self)
                 %# here I would like to do something in the lines of
                 funHandle = str2func(['@()self.process_' self.type])
                 funHandle() %# E1
          end
    end
    methods(private)
          %# I have a bunch of methods named process_[type]
          process_LSM(self)
          process_TIF(self)
          % etc...
    end
end

However, this does not work. On line E1 (above) MATLAB complains that class self is undefined and Java might not be running? Is there away to get this to work or do I have to use a switch structure in method process to call the type specific methods process_[type]?

2

There are 2 best solutions below

0
On BEST ANSWER

You need to use functional notation here, not dot notation. The following works:

funHandle = str2func(['@process_' self.type])
funHandle(self) %# E1
0
On

You may want to use feval instead:

feval(['@process_' self.type], self)