engineerchuan
engineerchuan

Reputation: 2708

Detect Numbers in Octave

I am trying the following right now:

function isNum = isItANum(string)  
    isNum = isempty(str2num(string))  
end

The problem is if I have a date '1998/34/2', I want my function to say no.

Upvotes: 0

Views: 273

Answers (2)

mtrw
mtrw

Reputation: 35098

From help str2num:

 *Caution:* As `str2num' uses the `eval' function to do the
 conversion, `str2num' will execute any code contained in the
 string S.  Use `str2double' instead if you want to avoid the use
 of `eval'.

 See also: str2double, eval

Looks like you can replace your function with ~isnan(str2double(string))

Upvotes: 1

Mascarpone
Mascarpone

Reputation: 2556

do a loop so that you split the string in single characters, and if any char fail, then return 0.

Upvotes: 0

Related Questions