如何确定字符串是否表示整数?

the*_*alk 3 matlab integer string-comparison

我需要确定一个字符串是否只包含一个整数.内置功能isinteger无效.

为了避免循环,我想在字符串的单元格数组上应用此任务.例如:

Q = { 'qf5' ; '4' ; 'true' ; 'false' ; '4.00' ; '4E0' ; '4e0' ; '657' }; 
Run Code Online (Sandbox Code Playgroud)

期望的结果:

integers = 0  1  0  0  0  0  0  1
Run Code Online (Sandbox Code Playgroud)

对于单个字符串,我想出了一个丑陋的解决方法,但我无法想象这是唯一可行的方法,并且它还需要一个循环才能在单元数组上使用它:

myString = '4';
integer = uint64( str2double( myString ) );
newString = int2str( integer );
isStringInteger = strcmp(newString,myString);
Run Code Online (Sandbox Code Playgroud)

我错过了哪个基本功能?

Lui*_*ndo 5

你可以做到regexp; 并避免使用你的循环cellfun:

~cellfun('isempty', regexp(Q, '^-?\d+$'))
Run Code Online (Sandbox Code Playgroud)

这将"整数"视为一串数字,可能在开头有一个减号.

请注意,cellfun内置函数'isempty'非常快.