Entering numbers from the keyboard

I want to input 4 numbers from the keyboard, then have the m file give me the largest and second largest numbers.
How do I check that the input is a number (and not a letter/other character) and how do I get the second largest number to be shown?
Also the programme should run continuously, taking 4 numbers at a time until the user wants to terminate, eg with an 'N' , how do I do this?
Thanks in advance

2 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 15 Juin 2013
What have you done so far? specify the problems you have encountered
Brian
Brian le 15 Juin 2013
Modifié(e) : Azzi Abdelmalek le 15 Juin 2013
xvalueone = 'enter a value for x';
xvalue = input(xvalueone);
a=xvalue
xvaluetwo = 'enter another value for x';
xvaluetwo = input(xvaluetwo);
b=xvaluetwo
xvaluethree = 'enter a third value for x';
xvaluethree = input(xvaluethree);
c=xvaluethree
xvaluefour = 'enter a 4th value for x';
xvaluefour = input(xvaluefour);
d=xvaluefour
A=[a,b,c,d]
C = max(A)
I don't know how to only allow input from numbers, how to keep the programme running until I terminate it or how to terminate it.
Thanks

Connectez-vous pour commenter.

Réponses (2)

Image Analyst
Image Analyst le 15 Juin 2013
Modifié(e) : Image Analyst le 15 Juin 2013

0 votes

Start with this:
% Ask user for 4 numbers.
defaultValues = {'1', '2', '3', '4'};
titleBar = 'Enter values';
userPrompts = {'Enter number #1', 'Enter number #2', 'Enter number #3', 'Enter number #4'};
% Loop until user clicks Cancel
while true
caUserInput = inputdlg(userPrompts, titleBar, 1, defaultValues);
if isempty(caUserInput),break,end; % Bail out if they clicked Cancel.
[value1, value2, value3, value4] = caUserInput{:}
value1 = str2double(value1)
value2 = str2double(value2)
value3 = str2double(value3)
value4 = str2double(value4)
% Check for a valid number.
if isnan(value1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
message = sprintf('value1 is not a number.\nI said it had to be an number.');
uiwait(warndlg(message));
end
if isnan(value2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
message = sprintf('value2 is not a number.\nI said it had to be an number.');
uiwait(warndlg(message));
end
if isnan(value3)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
message = sprintf('value3 is not a number.\nI said it had to be an number.');
uiwait(warndlg(message));
end
if isnan(value4)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
message = sprintf('value4 is not a number.\nI said it had to be an number.');
uiwait(warndlg(message));
end
end
Azzi Abdelmalek
Azzi Abdelmalek le 15 Juin 2013
Modifié(e) : Azzi Abdelmalek le 15 Juin 2013

0 votes

for k=1:4
xvalue='';
while ~isnumeric(xvalue)
test=0;
text=sprintf('enter a value n°%d for x',k);
str=input(text,'s');
id=regexp(str,'\d*\.?\d*','match')
if isempty(id)
id={' '};
test=1;
end
if numel(id{:})==numel(str) & test==0 & ~isequal(str,'.')
xvalue=str2num(str)
else
xvalue=''
end
end
A(k)=xvalue
end
c=max(A)

8 commentaires

Brian
Brian le 15 Juin 2013
Modifié(e) : Azzi Abdelmalek le 15 Juin 2013
I tried to get the second max number but could not, this is what I have:
for k=1:4
xvalue='';
while ~isnumeric(xvalue)
test=0;
text=sprintf('enter a value n°%d for x',k);
str=input(text,'s');
id=regexp(str,'\d*\.?\d*','match')
if isempty(id)
id={' '};
test=1;
end
if numel(id{:})==numel(str) & test==0 & ~isequal(str,'.')
xvalue=str2num(str)
else
xvalue=''
end
end
A(k)=xvalue
end
c=max(A)
c = max(A);
Y = A(A ~= c);
b = max(Y);
Also how do I make the programme run over and over again until a special input is entered?
Thanks
Image Analyst
Image Analyst le 15 Juin 2013
Why not just get all 4 numbers in one single call to inputdlg() like I showed you?
Azzi Abdelmalek
Azzi Abdelmalek le 15 Juin 2013
Modifié(e) : Azzi Abdelmalek le 15 Juin 2013
c = max(A);
Y = A(A ~= c);
b = max(Y);
is correct
you can also do
y=unique(sort(A))
c=y(end)
b=y(end-1)
I did'nt understand : make the programme run over and over again until a special input is entered
Brian
Brian le 15 Juin 2013
I originally used the code which gets all four in one go, however I couldn't make it keep rejecting answers until a number is entered
the best I could do was keep copying the second bit so it would reject a few times.
by this "make the programme run over and over again until a special input is entered" I mean I want it to keep asking for sets of 4 numbers and returning the highest and second highest in the array until the user decides to terminate the programme
Image Analyst
Image Analyst le 15 Juin 2013
All you have to do is put my code in a while loop. When you click Cancel it exits. See my answer - I edited it to add the while.
ask='enter 4 values: press 1 to quit any other key to continue'
q=input(ask,'s');
while ~isequal(q,'1')
for k=1:4
xvalue='';
while ~isnumeric(xvalue)
test=0;
text=sprintf('enter a value n°%d for x',k);
str=input(text,'s');
id=regexp(str,'\d*\.?\d*','match');
if isempty(id)
id={' '};
test=1;
end
if numel(id{:})==numel(str) & test==0 & ~isequal(str,'.')
xvalue=str2num(str);
else
xvalue='';
end
end
A(k)=xvalue;
end
y=unique(sort(A));
c=y(end)
b=y(end-1)
q=input(ask,'s');
end
Brian
Brian le 3 Sep 2013
Modifié(e) : Brian le 3 Sep 2013
The requirements are to also have the loop terminate when the letter "n" is inputted into any of the four boxes:
This is what I have now, mainly based on the original code from image analyst.
Tried to implement an if command for the termination by "n" but failed.
defaultValues = {'1', '2', '3', '4'};
titleBar = 'Enter values';
userPrompts = {'Enter number #1', 'Enter number #2', 'Enter number #3', 'Enter number #4'};
while true caUserInput = inputdlg(userPrompts, titleBar, 1, defaultValues); if isempty(caUserInput),break,end; [value1, value2, value3, value4] = caUserInput{:} value1 = str2double(value1) value2 = str2double(value2) value3 = str2double(value3) value4 = str2double(value4)
if isnan(value1) message = sprintf('value1 is not a number.\nI said it had to be an number.'); uiwait(warndlg(message)); end if isnan(value2) message = sprintf('value2 is not a number.\nI said it had to be an number.'); uiwait(warndlg(message)); end if isnan(value3) message = sprintf('value3 is not a number.\nI said it had to be an number.'); uiwait(warndlg(message)); end if isnan(value4) message = sprintf('value4 is not a number.\nI said it had to be an number.'); uiwait(warndlg(message)); end
end
a = [value1 value2 value3 value4] maxnumberis = max(a(:))
b=sort(a(:),'descend'); disp(b'); secondmaxnumber= b(2,1)
Image Analyst
Image Analyst le 4 Sep 2013
Please fix the formatting so we can copy and paste it.

Connectez-vous pour commenter.

Question posée :

le 15 Juin 2013

Community Treasure Hunt

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

Start Hunting!

Translated by