str2number - Fast replacement for str2num and str2double

Versión 1.0.0 (11,8 KB) por Yair Altman
Fast replacement for str2num and str2double; converts simple scalar input string(s) to number(s)
87 descargas
Actualizado 5 feb 2022

Ver licencia

str2number - Fast replacement for str2num and str2double; convert input string(s) to number(s)
result = str2number(str) % a single char/string input value
results = str2number(cellStrs) % cell-array of input char/string values
str2number converts char/string input value(s) to their numeric value(s).
This function can be much faster than str2num/str2double/sscanf for the common cases of simple numeric values. However, str2number cannot handle complex/imaginary values, exponents, and non-scalar values (as opposed to cell-arrays of scalar values, that are accepted). If you expect such non-supported values as inputs, then use str2num/str2double instead.
The input str argument can be a char array ('12.3'), or a string ("12.3"), or a cell array of chars/strings. The period ('.') is the expected decimal point.
The conversion result is a numeric value in case the input(s) can be converted into a numeric value. If an input cannot be converted into a number, then the original string is returned, like the builtin str2num function, and unlike the builtin str2double function (which returns NaN for unconvertable input values).
IMPORTANT: str2number was optimized for best performance, and often works faster than str2num/str2double/sscanf, but it only works for scalar non-complex, non-exponent values! See limitations below for details.
Limitations and differences compared to str2num/str2double:
- Inputs with a decimal sign different than a period ('.') are not converted
- Inputs with a thousands seperators (, or ' or .) are not converted
- Exponents (e.g. '1.2e3') are not converted
- Complex/imaginary values (e.g. '1+2i') are not converted
- Non-scalar values (e.g. '1,2' or '12 13') are not converted (but a cell-array of scalar values is acceptable)
- The original input string is returned if it cannot be converted into a number, unlike str2double (which returns NaN)
- Cell-arrays of chars and strings are accepted, unlike str2num
- String arrays are not accepted ; use str2double or convert to cell array
Usage examples:
str2number('12.34') % => [12.34] % scalar input value (char array)
str2number("12.34") % => [12.34] % scalar input value (string)
str2number('12,34') % => '12,34' % comma is not supported
str2number("1'234") % => "12'34" % thousands seperator is not supported
str2number('1.2e3') % => '1.2e3' % exponents are not supported
str2number('12+3i') % => '12+3i' % complex values are not supported
str2number('12 34') % => '12 34' % non-scalars are not supported
str2number({'12','34'}) % => {12,34} % cell-array of input values
str2number({'12','34','ab'}) % => {12,34,'ab'} % cell-array of input values
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)
See also:
str2num, str2double, sscanf

Citar como

Yair Altman (2024). str2number - Fast replacement for str2num and str2double (https://www.mathworks.com/matlabcentral/fileexchange/106265-str2number-fast-replacement-for-str2num-and-str2double), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2021b
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Versión Publicado Notas de la versión
1.0.0