It may help to write out the command separately (to a string) and then evaluate it, and so you will be able to see exactly what statement is being evaluated:
for i=1:3
for j=1:3
cmd = ['rent' num2str(i) '= landrent(i,j,Dist_lowcost);'];
fprintf('command to evaluate is: %s\n',cmd); % or just step through the code
eval(cmd);
end
end
The output from the above for i==1
is
command to evaluate is: rent1= landrent(i,j,Dist_lowcost)
command to evaluate is: rent1= landrent(i,j,Dist_lowcost)
command to evaluate is: rent1= landrent(i,j,Dist_lowcost)
Note that for every j
, we reset rent1
to landrent(i,j,Dist_lowcost)
and so that is why you are only getting three outputs - each subsequent iteration over j
replaces the previous result.
If you are determined to go ahead with the above and create new variables rather than using a matrix, you could do the following instead - create the renti
vector at each iteration of i
and then use that as you iterate over j
:
for i=1:3
cmd = ['rent' num2str(i) '=zeros(1,3);'];
eval(cmd);
for j=1:3
cmd = ['rent' num2str(i) '(j)= landrent(i,j,Dist_lowcost);'];
fprintf('cmd=%s\n',cmd);
eval(cmd);
end
end