function value = readInt(min,max,prompt) % READINT Get a user-input integer % readInt is cleaner version of MATLAB's INPUT function. % You can essentially bang on the keyboard without causing a problem. % Ensure that the prompt is a string: if ~ischar(prompt) error('Your prompt should be a string!'); end % Get initial input from the user: value = str2double(input(prompt,'s')); % Ensure that the input it legal: while ~isreal(value) | ... % no complex numbers isinf(value) | ... % no infinite values isnan(value) | ... % no NaNs ~isnumeric(value) | ... % no non-numerical values floor(value)~=ceil(value) | ... % no decimal/fraction values value > max | ... % lower bound of integer value < min % upper bound of integer % Reprompt the user and get revised input: disp('That value is not legal! Please re-enter: '); value = str2double(input(prompt,'s')); end