如何将字符串转换为函数名?

Bli*_*yir 2 matlab

我如何将x ='abs'转换为abs以便我可以执行z = abs(-5)= 5.或者其中x ='randfunc'其中'randfunc'可以是与函数相关的任何输入字符串.

>> x

x =

abs

>> x(-5)
Subscript indices must either be real positive
integers or logicals.
Run Code Online (Sandbox Code Playgroud)

Oli*_*rth 7

用途str2func:

x = 'abs';
fh = str2func(x);
fh(-5)               % Prints 5
Run Code Online (Sandbox Code Playgroud)