problem with symbolic expression
1 view (last 30 days)
Show older comments
Michal
on 24 May 2017
Commented: Walter Roberson
on 24 May 2017
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?
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!