MATLAB: How to prompt a user to input a number of values for their future inputts

cash flow

So right now, the code I have makes the user input up to an infinite number of values for the cash flow matrix and the intrest rates. How do I allow the user to first input the number of inputs they need, then individually ask the values for each yearly cash flow as well as the interest rates?
clc;
clear;
close all;
A= input('Input cash flow matrix in the form "[yr1 yr2 yr3 yr...] \n"-->'); %Defines a initially after buying the machine
n=length(A);
for i =1:n
x(i) = i-1;
end
PW= 0;
int=input('Input various intrest rates in the form [a b c...]\n-->');
bar(x,A,0.25)
hold off
%Equation for PW analysis
for j= 1:length(int)
PW=0;
for t= 1:n
PW=PW+A(t)*int(j)^(-x(t));
PWA(j,t)=PW;
end
end
%plots CF diagram
figure(1)
hold on
plot(x,PWA,'-x','LineWidth',2)
legend('CF diagram','Location','southeast')
Current output:
Input cash flow matrix in the form "[yr1 yr2 yr3 yr…]"
–>[-5000 2000 2000 1000] *note these are random numbers I input
Input various intrest rates in the form [a b c…]
–>[1.01 1.02] *note these are random numbers I input

Best Answer

Mallard - use a for loop. For example,
numYears= input('please enter number of years: ');
cashFlows = zeros(numYears, 1);
interestRates = zeros(numYears, 1);
for k = 1:numYears
cashFlows(k) = input(sprintf('input cash flow for year %d: ', k));
interestRates(k) = input(sprintf('input interest rate for year %d: ', k));
end
The above assumes that the user enters in the same number of interest rates as cash flow values.