Info

This question is closed. Reopen it to edit or answer.

need help to make program recognize users inputs and associate values of a matrix to that users input

1 view (last 30 days)
im writting a program that measures the average water level at a dam for a period of 8 years and i need to ask the user to input a year that he or she would like to examine, the program needs to recognize if the years a valid option and associate it with that years average water levels and then compare the users input with the year before and after, how do i make the program recognize say if the user inputs the year 2000, the program would know its a valid option and then show the average water levels for that year thats in a matrix.
thank you for your time and consideration.
  5 Comments
Image Analyst
Image Analyst on 24 Feb 2013
Ok, I was just trying to figure out which one to answer, but now I won't worry about either question. Leaves me time to answer lots of other questions. Good luck though.

Answers (1)

Shaun VanWeelden
Shaun VanWeelden on 24 Feb 2013
Hey Stephen,
I did not look at your apparent other question, but to validate the year is very straightforward. I would do something like this:
minYear=1995
maxYear=2005
userYear=input('Enter the year you would like to examine');
while userYear<minYear || userYear>maxYear
fprintf('Please enter a year between %.0f and %.0f\n',minYear,maxYear)
userYear=input('Enter the year you would like to examine');
end
now if you had a lot of years, I would recommend putting them in a 3-D matrix, with each 2-d slice being a year, but since you only have 8, I would recommend just using the switch statement, which would go something like this:
switch userYear
case 1995
do something with 95 matrix
case 1996
do something with 96 matrix
....
end
You could alternatively just use the menu function possibly to select the year.

Community Treasure Hunt

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

Start Hunting!