problem with symbolic expression

3 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 24 May 2017
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 Comments
Michal
Michal on 24 May 2017
Edited: Michal on 24 May 2017
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 on 24 May 2017
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!