Using variable index in matlab

66 Views Asked by At

If we have k=1:n, and then we put k in a function so we get fk=f(tk) for some values of tk, when I apply the program I get just fk for each value of k, it is not named by f1, f2. Here is the statements

for k=1:n
t=linspace(0,0.25)
tk=t.*(k-1)/n
f=inline('x.^0.2')
fk=f(tk)
1

There are 1 best solutions below

2
On BEST ANSWER

Normally, it is not a good idea to create new variables in a loop. It is better to have an array or a cell array. However, if you really need it, then one possible solution is:

n = 5;
t = linspace(0,0.25);
f = @(x) x.^0.2;
for k = 1:n
    tk = t.*(k-1)/n;
    s = ['f' num2str(k) ' = f(tk);'];
    evalin('caller',s);
end

I avoide inline because it will be removed.