from
StringIsNumber
by Michael Robbins STRINGISNUMBER returns 1 if the input string contains only a number and otherwise a zero.
out=StringIsNumber(instr)
function out=StringIsNumber(instr)
% 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('aseeea')
% ans = 0
%
% >> StringIsNumber('123')
% ans = 1
%
% >> StringIsNumber('123.123')
% ans = 1
%
% >> StringIsNumber('a123.123')
% ans = 0
%
% >> StringIsNumber({'123','12.2','a123.123','abc'})
% ans = 1 1 0 0
%
% >> [StringIsNumber('100.') ...
% StringIsNumber('100.1') ...
% StringIsNumber('.1') ...
% StringIsNumber('.') ...
% StringIsNumber('100') ...
% StringIsNumber('100a') ...
% StringIsNumber('dca')]
% ans = 1 1 1 0 1 0 0
%
% IT'S NOT FANCY BUT IT WORKS
% Michael Robbins
% robbins@bloomberg.net
% michael.robbins@us.cibc.com
temp=regexprep(instr, ...
'((\d+\.\d+)|(\d+\.)|(\.\d+)|\d+)','');
if iscellstr(instr)
out =~cellfun('isempty',instr) ...
& cellfun('isempty',temp);
else
out = ~isempty(instr) ...
&& isempty(temp);
end;