Trouble with char and doubles in code

4 views (last 30 days)
I'm trying to write some code that asks a user for two numbers and checks that the second is not less than the first.
If the user enters anything other than a number they should be prompted to try again.
I'm having trouble distinguishing between char and double and I now have an error that I am not sure what it means.
Here is my code:
A = ''; %char
while isempty(str2num(A))
A= input('Please enter a number: ','s');
end
A = str2num(A); %double
B = ''; %char
while true
while isempty(str2num(B)) %char
B= input('Please enter a number: ','s'); %char
end
while ~isempty(str2num(B)) %char
B = str2num(B); %double
if B < A %double
B= input('B less than A, try again: ','s'); %char
else
B = num2str(B); %char
break
end
B = num2str(B); %char
break
end
end
B = str2num(B); %double
%need A and B to be doubles for plotting......
Here is the error:
EDU>> Untitled2
Please enter a number: 3
Please enter a number: 6
Operation terminated by user during int2str (after line 32)
In num2str (line 68)
s = int2str(x); % Enhance the performance
In Untitled2 (line 19)
B = num2str(B); %char
Any help would be much appreciated as Matlab has been outwitting me for the last eleven hours.
Cheers
Steve

Accepted Answer

StevieNewRoad
StevieNewRoad on 23 Oct 2013
Thank you for the quick reply and taking time to address my question. I see the problem that I have with the while loop and will have a go at fixing it. The reason that I have gone for coding in this way is to try to avoid the automatic Matlab error that is displayed when anything other than a number is entered into a double:
Error using input
Undefined function or variable 'h'.
Error in Untitled3 (line 1)
A= input('enter a number ');
Earlier today I asked another question to see if there was a way of suppressing this error before being guided along the lines of the code that you have perused. I have written some code that works with the error, but if symbols such as & or £ are entered the program exits:
A= input('enter a number ');
while isempty(A)== 1
A= input('try again ');
end
B= input('enter a number ');
while true
if isempty(B)== 1;
B= input('try again ');
elseif B < A;
fprintf('B must be greater than A %.2f - enter a value ',A);
B= input('');
else
break
end
end
I’ve tried to use the isnan() function but if I don’t really get it:
a = input('enter number: ') %double
b = isnan(a)
enter number: 2
a =
2
b =
*0*
enter number: f
Error using input
Undefined function or variable 'f'.
Error in Untitled2 (line 30)
a = input('enter number: ')
a = input('enter number: ','s') %string
b = isnan(a)
enter number: 4
a =
4
b =
*0*
enter number: d
a =
d
b =
*0*
??????
  1 Comment
Walter Roberson
Walter Roberson on 23 Oct 2013
A = str2num( input('enter a number ', 's') );
if numel(A) == 1 && ~isnan(A)
... try was okay ...
end

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Oct 2013
When you get to the "else" of "if B < A", you convert B back into a string and then "break" out of the "while ~isempty" loop. But that leaves you inside the "while true" loop. Which goes through the logic again, converts B to numeric, does the test, hits the else, converts B back to string, breaks the ~isempty loop, stays within the while true loop...
I would also point out that you never test whether A or B are strings that represent numbers. You can do that by testing isnan() of the numeric value. Remember to get a confirmed number for A before you bother getting a confirmed number for B. And remember that once you have two confirmed numbers you are allowed to end the program (after doing the check for which is bigger), so you do not need to infinite loop the whole process.

Tags

Community Treasure Hunt

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

Start Hunting!