Converting string to function/number
9 ビュー (過去 30 日間)
古いコメントを表示
Hello, sorry to bother your time. So in this case, my problem is, i'm trying to design an app using app designer that can take user input in string and convert it into numbers. However, when i try to use some mathematical function like 'sin, cos, exp' etc, as an input, i want those strings to be converted into the mathematical function as they were stated. How can i achieve this?
2 件のコメント
Stephen23
2022 年 5 月 27 日
編集済み: Stephen23
2022 年 5 月 27 日
"How can i achieve this?"
If you are interested in writing debuggable, robust, secure code and have only a limited number of such functions then use SWITCH or a cell array of explicit function handles or similar.
If you are happy with the risks of allowing the user to be able to run anything then use STR2FUNC.
回答 (3 件)
Rik
2022 年 5 月 26 日
The way to avoid eval in this case is to use str2func:
f=str2func('cos');
f(pi)
0 件のコメント
Walter Roberson
2022 年 5 月 27 日
str2sym()
matlabFunction is commonly used together with str2sym()
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!