problem with symbolic expression

1 回表示 (過去 30 日間)
Michal
Michal 2017 年 5 月 24 日
コメント済み: Walter Roberson 2017 年 5 月 24 日
A have some troubles with conversion of character vector to symbolic expression under R2017a:
% character vector
e = '(-1)*(((2.1967)-(x3))*((x4)+(x2)))'
% symbolic expression
sym(e)
Warning: Support of character vectors that are not valid variable names or define a number will be removed in a future release. To
create symbolic expressions, first create symbolic variables and then use operations on them.
> In sym>convertExpression (line 1586)
In sym>convertChar (line 1491)
In sym>tomupad (line 1243)
In sym (line 199)
ans =
(x3 - 2.1967)*(x2 + x4)
How to solve this problem to be fully compatible with future releases?

採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 24 日
You cannot solve it to be fully compatible with future releases. You will need to stop doing that.
Or you could cheat:
e = '(-1)*(((2.1967)-(x3))*((x4)+(x2)))'
vars_char = symvar(e);
vars_sym = sym(vars_char);
vars_cell = num2cell(vars_sym);
fun_str = ['@(', strjoin(vars_char, ','), ') ', e];
fun_handle = str2func(fun_str);
result = fun_handle(vars_cell{:});
However, this will not be exactly the same, in that numeric values will be calculated out, and floating point numbers will typically be converted to rational. For example, 'sin(1) + x' would come out as x + 3789648413623927/4503599627370496
  2 件のコメント
Michal
Michal 2017 年 5 月 24 日
編集済み: Michal 2017 年 5 月 24 日
This change completely broke my whole code which is very complex. I need to find some workaround, because in opposite case I will need to completely rewrite my whole code (~ 5000 lines of code). This would be really terrible thing.
Walter Roberson
Walter Roberson 2017 年 5 月 24 日
I would recommend that you create a support case setting out your reasons why your expressions must be strings, and pushing them for a solution.
Every once in a while, they reverse a decision on removing a feature, if the customers establish that the functionality has no practical work-around.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by