how to prompt user to try again instead of letting MATLAB display "Index in position 1 exceeds array bounds" error message.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammed Mustafa
am 24 Jul. 2021
Kommentiert: Mohammed Mustafa
am 24 Jul. 2021
hello, I am trying to index an element from an array say
a= [1 2 3;
4 5 6;
7 8 9];
position = input ('input a number: ')
element=a(postion,:)
if the user enters a value bigger than the size of array 'a' MATLAB prints the following:
"Index in position 1 exceeds array bounds" and stops.
my question is how do I print ("enter a valid input") and prompt the user to try again.
Thanks.
0 Kommentare
Akzeptierte Antwort
Ive J
am 24 Jul. 2021
a= [1 2 3;
4 5 6;
7 8 9];
askAgain = true;
while askAgain
position = input ('input a number: ');
if position > size(a, 1)
disp('wrong choice, try again!')
else
askAgain = false;
end
end
element=a(postion,:)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!