Display all while loop outputs into a chart?

So I have a bank account of 6000 dollars and it has a 12% annual interest rate. I have to stop the while loop when the 6000 doubles. Just having some trouble displaying this as a chart with one column counting years and the other displaying the balance after each year. This is what I have right now but it only displays the final year and final balance. Please help!
Balance = 6000;
Year = 0;
while (Balance < 12000)
Year = Year + 1;
Balance = Balance*1.12;
end
T = table(Year, Balance)

Answers (1)

You need to subscript them and save them as vectors:
Balance = 6000;
Year = 0;
k = 1;
while (Balance < 12000)
k = k+1;
Year(k) = Year(k-1) + 1;
Balance(k) = Balance(k-1)*1.12;
end
T = table(Year(:), Balance(:))
figure(1)
plot(Year, Balance)
grid on

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 4 Mar 2018

Answered:

on 4 Mar 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!