How to identify strings that can be converted to float numbers in a cell array?

4 次查看(过去 30 天)
I can convert a string to double using str2double, but my goal is to identify such cells. Attached is an example file.
A = readcell('test6.xlsx');
As you can see, some cells on the 2nd column meet this criteria.
Many thanks.

采纳的回答

Les Beckham
Les Beckham 2025-3-11
I would suggest using readtable instead of readcell
A = readcell('test6.xlsx')
A = 9x6 cell array
{'Cruise ID'} {'Salinity_flag'} {'Longitude_DEC'} {'Temp' } {'Salinity'} {'EXPO' } {'ID12' } {[ 2]} {[ -25.0100]} {[ 27.7690]} {[ 35.5800]} {'33RC1991'} {'ID12' } {[ 2]} {[ -25.0020]} {[ 25.9660]} {[ 35.3670]} {0x0 char } {'ID12' } {0x0 char } {[ -24.9930]} {[ 25.0250]} {[ 35.8480]} {[ 200105]} {0x0 char } {0x0 char } {[ -25.0010]} {[<missing>]} {0x0 char } {[ 456]} {'ID12' } {'2' } {[ -25.0020]} {[ 25.3260]} {[ 36.0880]} {[ 3689]} {'ID12' } {'2' } {[ -25]} {[ 26.1690]} {[ 35.9120]} {[ 3256778]} {'ID12' } {'2' } {[ -25]} {[<missing>]} {[ 36.2220]} {'33RC1991'} {'ID12' } {[ 2]} {[ -25]} {[<missing>]} {[ 36.3660]} {'33RC1991'}
You can define the variable types so Matlab knows which columns (variables) are supposed to be numeric.
opts = detectImportOptions('test6.xlsx');
opts.VariableTypes = { 'char', 'double', 'double', 'double', 'double', 'double' };
T = readtable('test6.xlsx', opts)
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = 8x6 table
CruiseID Salinity_flag Longitude_DEC Temp Salinity EXPO __________ _____________ _____________ ______ ________ __________ {'ID12' } 2 -25.01 27.769 35.58 NaN {'ID12' } 2 -25.002 25.966 35.367 NaN {'ID12' } NaN -24.993 25.025 35.848 2.001e+05 {0x0 char} NaN -25.001 NaN NaN 456 {'ID12' } 2 -25.002 25.326 36.088 3689 {'ID12' } 2 -25 26.169 35.912 3.2568e+06 {'ID12' } 2 -25 NaN 36.222 NaN {'ID12' } 2 -25 NaN 36.366 NaN
And, if you really want a cell array instead of a table:
C = table2cell(T)
C = 8x6 cell array
{'ID12' } {[ 2]} {[-25.0100]} {[27.7690]} {[35.5800]} {[ NaN]} {'ID12' } {[ 2]} {[-25.0020]} {[25.9660]} {[35.3670]} {[ NaN]} {'ID12' } {[NaN]} {[-24.9930]} {[25.0250]} {[35.8480]} {[ 200105]} {0x0 char} {[NaN]} {[-25.0010]} {[ NaN]} {[ NaN]} {[ 456]} {'ID12' } {[ 2]} {[-25.0020]} {[25.3260]} {[36.0880]} {[ 3689]} {'ID12' } {[ 2]} {[ -25]} {[26.1690]} {[35.9120]} {[3256778]} {'ID12' } {[ 2]} {[ -25]} {[ NaN]} {[36.2220]} {[ NaN]} {'ID12' } {[ 2]} {[ -25]} {[ NaN]} {[36.3660]} {[ NaN]}
  9 个评论
Les Beckham
Les Beckham 2025-3-11
Note that str2double will give you the same mask. It returns NaN for anything that it can't convert.
A = readcell('test6.xlsx');
mask = ~isnan(str2double(A))
mask = 9x6 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
Walter Roberson
Walter Roberson 2025-3-11
Yes. You specifically wanted a method that did not use str2double() (for reasons that are not clear to us).

请先登录,再进行评论。

更多回答(1 个)

Leon
Leon 2025-3-11
编辑:Leon 2025-3-11
Can it be as simple as this?
>> str2double(A)
ans =
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
NaN 2 NaN NaN NaN NaN
NaN 2 NaN NaN NaN NaN
NaN 2 NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN
map0 = ~isnan(B)
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0 0 0 0
0 1 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by