No BSD License  

Highlights from
StringIsNumber

3.0

3.0 | 1 rating Rate this file 2 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
expression, number, regex, regexp, regexprep, regular, string, strings
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
08 Jul 2010 Jan Simon

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

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' ).

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.

Contact us