No BSD License  

Highlights from
StringIsNumber

3.0

3.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 1.1 KB File ID: #6283
image thumbnail

StringIsNumber

by Michael Robbins

 

15 Nov 2004 (Updated 22 Nov 2004)

STRINGISNUMBER returns 1 if the input string contains only a number and otherwise a zero.

| Watch this File

File Information
Description

STRINGISNUMBER returns 1 if the input string contains only a number and otherwise a zero
 
Created as an example for the thread "How to determine if an inputfield contains chars or numbers ?"
 
  USAGE:
  >> StringIsNumber('aseea')
  ans = 0
  >> StringIsNumber('333')
  ans = 1
  >> StringIsNumber('333.123')
  ans = 1
  >> StringIsNumber('.123')
  ans = 1
  >> StringIsNumber('0.123')
  ans = 1
  >> StringIsNumber('0.123a')
  ans = 0
  
  IT'S NOT FANCY BUT IT WORKS

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
07 Jul 2006 Jaroslaw Tuszynski

Simple robust function. Does not recognize negetive numbers and scientific notation. Nice alternative to ~isempty(str2num(str)) approach which sametimes gives strange results (try str = 'sphere' ).

08 Jul 2010 Jan Simon

Another idea:
  [x, count, err, next] = sscanf(Str, '%g', 1);
  isNumber = (count == 1) && (next == numel(Str) + 1);

Please login to add a comment or rating.
Updates
18 Nov 2004

A single period is no longer considered a number. It must be succeeded by a digit.

Vector inputs and outputs are supported.

22 Nov 2004

Digits followed by a period but no more digits is now considered a number.

A single period is no longer considered a number.

Vector inputs and outputs are supported.

Tag Activity for this File
Tag Applied By Date/Time
strings Michael Robbins 22 Oct 2008 07:35:49
string Michael Robbins 22 Oct 2008 07:35:49
number Michael Robbins 22 Oct 2008 07:35:49
regular Michael Robbins 22 Oct 2008 07:35:49
expression Michael Robbins 22 Oct 2008 07:35:49
regex Michael Robbins 22 Oct 2008 07:35:49
regexp Michael Robbins 22 Oct 2008 07:35:49
regexprep Michael Robbins 22 Oct 2008 07:35:49

Contact us at files@mathworks.com