gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:22:51 25 4
gpt4 key购买 nike

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

>> x

x =

abs

>> x(-5)
Subscript indices must either be real positive
integers or logicals.

最佳答案

使用 str2func :

x = 'abs';
fh = str2func(x);
fh(-5) % Prints 5

关于matlab - 如何将字符串转换为函数名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com