I am wondering how to make symbolic variables out of already defined strings. It actually will help us to make this code run:
N=2
for i=1:N
syms sprintf('r%g',i)
end
This error is appeared after running it:
Error using symfun.parseString (line 50)
Not a valid variable name.Error in syms (line 166)
[name, vars] = symfun.parseString(x);
I want it to create r1
and r2
as two symbolic variables.
The issue is because if you want to pass a string to syms
you need to use the function syntax rather than the command syntax. With your current syntax (command syntax), MATLAB is trying to create a symbolic variable named 'sprintf('r%g',i)'
and doesn't actually evaluate the sprintf
.
Instead the function syntax should look like the following. Also, you should use %d
in your format string to ensure that i
is an integer otherwise it won't be a valid variable name.
syms(sprintf('r%d', i))