Arithmetic to Functional Converter
1 回表示 (過去 30 日間)
古いコメントを表示
I was wondering if anyone knows of a way to convert arithmetic to functional synatx. For example, I'd like to convert the string
'a+b'
to
'plus(a,b)'
Now, this was a very simple example. It gets a bit harder with more "complicated" expressions.
'a+b.*c'
becomes:
'plus(a,times(b,c))'
I assume MATLAB already does this, under the hood somewhere. There should be a way (either documented or not) to access this. Any insights?
0 件のコメント
採用された回答
Walter Roberson
2012 年 3 月 26 日
You might be able to get something useful by hacking the source to mtree:
その他の回答 (3 件)
owr
2012 年 3 月 27 日
Are you just trying to evaluate algebraic statements provided in string format?
If so, try "eval":
>> a = 4
a =
4
>> b = 6
b =
6
>> str = 'a+b'
str =
a+b
>> eval(str)
ans =
10
John D'Errico
2012 年 3 月 27 日
Its easy, IF you look at this the right way.
First, pre-parse the expression to determine that 'a+b.*c' has three variables in it, a, b, and c. Define objects a, b and c. Now overload the plus and times operators to actually use bsxfun.
Finally, let MATLAB do the operation, using an eval. You CAN make this work, but you will need to be careful and think it out.
anisa
2012 年 5 月 31 日
Hi, Did you manage to find a method in MATLAB that already implements this. I am trying to do the same thing I am sure that somewhere in matlab there should already be a method. If you could let me know that would be greatly appreciated.
1 件のコメント
Walter Roberson
2012 年 5 月 31 日
I, on the other hand, am "sure" that there is no documented MATLAB method to do this (other than what John outlined.)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!