Converting strings to operators
古いコメントを表示
I think this is a naive query. Yet, let me pose it:
If '(' '2' '*' '3' ')' are elements of a string vector S, then what function f results in the following:
f(S) = (2*3) = 6
str2num for '2' and '3' is not an option, since they can occur anywhere in S, and there is no apriori information as to where they will occur.
For the same reasons, keeping the bracket structure intact is also important, even though it seems that we can do away with the brackets in this example.
採用された回答
その他の回答 (1 件)
I can't tell if you really intend S to be a string vector because that dpesn't agree with the example you posted. If I assume it is what you meant, though, then you could do,
S=["(" "2" "*" "3" ")"];
whos S
eval(cell2mat(cellstr(S)))
1 件のコメント
Walter Roberson
2020 年 11 月 26 日
If this is user input then evalc is dangerous as the user could have coded a call to delete files
カテゴリ
ヘルプ センター および File Exchange で Standard File Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!