MATLAB: How to get the variable from inside of the for loop and use it /display it after for loop

for loop

Hi,
I created a for loop. Inside this for loop I calculated a variable. Unfortunately Matlab deletes the variable after the end of the for loop. How can I use the calculated variable outside of the for loop? Thanks!

Best Answer

That depends where your loop is, in script file or function file If it's in Function file, that have nothing to do with a loop , you declare your variable global, If it's in the script file, your variable can't be deleted. look at these examples
for k=1:10
x=sin(k)
end
the result is x=sin(10); the previous x where erased, if you want to save them
for k=1:10
x(k)=sin(k)
end
Related Question