Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

isnumber?

1,629 views
Skip to first unread message

mark b.

unread,
Mar 2, 2008, 11:23:03 AM3/2/08
to
hi, i cannot find anything in matlab-help to check if smth
is a number. i need to compare if user wrote a number or a
letter.

if i try 'isnumeric' it doesn't work as i want to. for
isnumeric(1) and isnumeric(a) it returns true..

is there smth similar to isnumber in matlab?

Walter Roberson

unread,
Mar 2, 2008, 11:52:35 AM3/2/08
to
In article <fqek97$s4a$1...@fred.mathworks.com>,

mark b. <john.do...@mathworks.com> wrote:
>hi, i cannot find anything in matlab-help to check if smth
>is a number. i need to compare if user wrote a number or a
>letter.

ischar() to test for characters.

See also isfinite() and isnan()
--
"All human knowledge takes the form of interpretation."
-- Walter Benjamin

Dave Bell

unread,
Mar 2, 2008, 11:58:28 AM3/2/08
to

In your examples, <1> is a numeric constant, and test as numeric.
But <a> is the name of a variable, that probably *contains* a numeric value.
If you try isnumeric('a'), you'll see it returns 0.
Further, a='abc'; isnumeric(a) will return 0,
while b=123; isnumeric(b) will return 1.

But be careful how you have the user enter the text.
If you do, c='123'; isnumeric(c) will return 0, because <c> contains a
string that just happens to be all digits; it is not a number!

Dave

mark b.

unread,
Mar 2, 2008, 1:23:02 PM3/2/08
to
thx!

hello_e...@arcor.de

unread,
May 6, 2013, 9:10:05 AM5/6/13
to
since this still comes up pretty close to top in google, here is another and quite functional approach: try matlab regexp.

for example for only digits you can try pattern /\d+/

Paulo Abelha

unread,
Sep 16, 2016, 12:26:10 PM9/16/16
to

Benjamin Askelund

unread,
Sep 21, 2016, 2:42:13 PM9/21/16
to

Hi there, this might help you out. I made a short code that takes in a number (or string) from a user and returns the number if it sure is a number or 0 if it is a string.

copy and paste in a "script" to try out. Save the script and then test it in comand window.


CODE:

number = input('Type a number please = ', 's');
% this pice of code can take in both numbers or strings:

number = str2num(number);
%If the string "number" does not represent a valid number or matrix,
str2num(number) returns the empty matrix.

if isempty(number)
answer = 0;
else
answer = daysInMonth(number);
end

disp(answer)

ENDCODE

or:


number = input('Type a number please = ', 's');
number = str2num(number);

if isempty(number)
disp('You wrote a text')
else
fprintf(' You wrote the number %2.0f\n ', number)
end
0 new messages