MATLAB: How to access a symbolic variable created by sym

MATLABmatrix manipulationsolvesymbolic

Basically I need to create two sets of "y" symbolic variables that are defined by the user. Doing so with the function
u=sym('u',[y 1]);
r=sym('r',[y 1]);
After running, asking for r(1) returns r1, but asking for r1 gives the error "Undefined function or variable 'r1'.". This code is meant to end up with three matrices: u, r and K so that K*u=r, where K a numerical double symmetrical matrix. I'm assigning some values to some of the matrices so that the u matrix at the end is [0; u2; u3; u4;0] and the r is [r1;0;1000;0;r5] but I can't find a way to solve this problem. Using the function
s=solve(K*u==r,u,r)
returns the error
Error using sym.getEqnsVars>checkVariables (line 88)
The second argument must be a vector of symbolic variables.
Does anyone have any idea how to properly assign these variables and how to solve the system at the end? Thank you in advance.

Best Answer

subs(expression, u([1 4]), [0 0])
In most circumstances it is better to avoid assigning a value to an existing symbolic variable that has been used in an expression.