Converting strings to operators
Show older comments
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.
Accepted Answer
More Answers (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 Comment
Walter Roberson
on 26 Nov 2020
If this is user input then evalc is dangerous as the user could have coded a call to delete files
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!