MATLAB Answers


Error in sym: Character vectors and strings in the first argument can only specify a variable or number.

Asked by Tejas Adsul on 31 May 2018
Latest activity Commented on by Aleksei Tepljakov on 12 Apr 2019
So I have this simple code
x = sym('x',[5,1]);
T1 = sprintfc('x%d(t)', 1:5); T1=T1'; syms(T1{:});
eqn = x(1)^2 + x(2) + x(3)/3 + x(4) - x(5);
This used to work perfectly before. Now, I'm using Matlab 2018a. It gives me this error:
Error using sym>convertChar (line 1448)
Character vectors and strings in the first
argument can only specify a variable or
number. To evaluate character vectors and
strings representing symbolic expressions,
use 'str2sym'.
Error in sym>tomupad (line 1214)
S = convertChar(x);
Error in sym (line 211)
S.s = tomupad(x);
Error in sym/subs>@(x)sym(x) (line 209)
Y =
Error in sym/subs>normalize (line 209)
Y =
Error in sym/subs>mupadsubs (line 157)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
What should I change in this so that I get the required output? I think I have to use syms x1 x2 etc, instead of sym. However, the number of symbolic variables is random, and I don't know how to create symbolic variables in a loop using syms.





3 Answers

Answer by Walter Roberson
on 31 May 2018

MATLAB has been warning for a number of releases that it was no longer going to be possible to syms() an expression. R2018a is the release that they pulled the plug. str2sym is the replacement.
The difference is that str2sym takes a MATLAB expression as input, whereas sym() used to take a mupad expression as input.


Sign in to comment.

Answer by Aleksei Tepljakov on 21 Nov 2018
Edited by Aleksei Tepljakov on 21 Nov 2018

This is ludicrous and equally outrageous. Introduce a new feature in R2017b, give up on a feature in R2018a. Who does that? Where's the time to adopt existing code?
I made a simple compatibility fix in the code that I now maintain so that it works with simple functions in both pre- and post-R2017b. However, here comes the surprise: it still doesn't fix the overall issue apparently because str2sym doesn't support custom functions. So if I say, for example, str2sym('x1+custom_fun(x2)') where custom_fun is a m-function in the cwd or on MATLAB path, the result is an error: “Unable to convert string to symbolic expression: Invalid data type. Input arrays must be numeric or logical.” This completely breaks the symbolic features of the toolbox along with my applications.

  1 Comment

Solution to my problem, as implemented in the form of a compatibility function in GPTIPS2F:
Achieved by using ugly crutches & also contacting MathWorks support (they were helpful). Still, perhaps someone will find this useful.

Sign in to comment.