From: Kyle on
I want to store a range of variables to be used throughout various function files. I have a range of values for example x1, x2, x3, x4, etc. How can I do this? Here is my code:

for n=1:100;
if n<1
ERROR
elseif n<=100;
x=5+2n
else n>100
ERROR
end
end
From: Matt Fig on
If you ever hit one of those errors, MATLAB has a MAJOR bug.

x = zeros(1,100);
for n=1:100
x(n) = 5+2*n;
end

% Or, simply:

x2 = 5 + 2 * (1:100);
isequal(x,x2)
From: Kyle on
Well this is just a simplified case. That code works with n=1:100 but what if the numbers weren't as nice like that? Like if n=[3.2,2.8,0.21] then the "x(n)" code will not work since the index must be a positive integer or logical. I've tried using this method:

for k = 1:10
data = x*y*z*k; %% for example
eval([ sprintf('v%d = data;', k)])
end

But I cannot modify it to work with my code. Any suggestions?
From: Matt Fig on
There are several ways to tackle this one:

% Data
n = [3.2,2.8,0.21];

% Engine - omitting pre-allocation of x this time
cnt = 0;
for ii = n
cnt = cnt + 1;
x(cnt) = ii.^2;
end