How do i use str2func for methods within a class?

8 visualizaciones (últimos 30 días)
Jimmy
Jimmy el 16 de Feb. de 2018
Respondida: Suraj Mankulangara el 22 de Feb. de 2018
I have a class with several functions. Now I want to write a new function within this class that has an string as an input. This string is an actual function name that is defined within that same class. The new function has to convert this string to the function handle and run this particular function. I tried to do this conversion using str2func, however it doesnt recognize the function: Undefined function or variable 'obj.calcMassMatrixTot'.. However, when I try a global Matlab function such as rand, it works. So it looks like str2func can only see functions that are defined globally. How should I approach this problem?
edit: it might be important to note that im creating the function handle in a function within a different class.
edit2: solved it by moving the new function to the class where i am running all the functions. Still i wonder how i could make the right function handle.

Respuesta aceptada

Suraj Mankulangara
Suraj Mankulangara el 22 de Feb. de 2018
Hello Jimmy
Here is my understanding of what you are trying to achieve:
1. Class A has a function, let's say, functionA
2. You want to write a function, say, functionB within a different Class, B, which accepts string as input.
3. You want to pass the string 'functionA' as input to functionB, use str2func on the string 'functionA'. This would get you the handle to functionA. And subsequently, you want to invoke functionA using this handle, from within functionB
The issue here, is that functionA is a member of Class A, and can only be invoked on objects of Class A. The error message that you have posted suggests to me that you are trying to invoke functionA on an object of class B, which wouldn't work unless Class B derives from Class A.
The solution is to also pass an object of A to functionB. Once the function handle of functionA is retrieved, you can invoke functionA on the object of A that has been passed to functionB.
The following code should make this evident:
classdef A % Definition of class A
methods
function printString(obj, string)
disp(string);
end
end
end
Now create an object of A:
x = A;
x.printString('Hello') % prints 'Hello'
Class definition of B
classdef B
methods
function testOther(obj, otherObj, functionName, stringToBePrinted)
fh = str2func(functionName);
fh(otherObj, stringToBePrinted);
end
end
end
Create an object of B, and pass object of A, and the name of the function as arguments:
y = B;
testOther(y, x, 'printString', 'Hello') % This invokes x.printString() from within an object of B and prints 'Hello'
There are a couple of things that you might want to look at:
  • In your case, you worked around this problem by incorporating 'functionB' in class A instead of class B. This works since 'functionA' and 'functionB' are both operating on objects of the same class, and thus, are visible to each other. However, you probably need to look at your design and decide whether it really is a good idea to include functionB in class A.
  • When you pass objects of class A into functionB, you need to take care that the right object, i.e. an object with the right state, is passed.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by