problem with symbolic expression

조회 수: 3 (최근 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