Search code examples
matlabfor-loopsymbolic-math

Using A For Loop To Create Symbolic Variables


I am trying to create a large number of symbolic variables using a for loop, so that I do not have to type each individual variable. Here is my attempt:

for i 1:19
    yi = sym('yi');
end

However, I am getting this error: Unexpected MATLAB expression.


Solution

  • I don't have access to the Symbolic Math Toolbox but see if this helps:

    for i=1:19
     eval(sprintf('y%d = sym(''y%d'')', i,i))
    end 
    

    Although, I highly recommend against doing it this way.