problem with symbolic expression

1 次查看(过去 30 天)
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