How to convert a column of float or not data into double?

4 次查看(过去 30 天)
My data column can be float numbers throughout, e.g.
A = [1, 2, 3];
or a strings, like the below
B = [1, 2, 3, 'n/a', 5];
Is there a universal function that can convert both into numerical values? Or do I have to use a loop to check if they are float or not?

回答(3 个)

Steven Lord
Steven Lord 2025-6-6
Are you trying to import this data from a file? If so please show us (or tell us, if you can't provide the code) exactly how the data looks in the file and which tools / functions you're using to import it. Depending on the specific tool there may be an option to control what gets imported as missing data.
If you want to type this and have MATLAB automagically know that it ought to replace the 'n/a' with NaN, there is no such function. Instead what will happen is that MATLAB will convert the three characters in 'n/a' into their numerical values and store those numerical values in the numeric vector.
B = [1, 2, 3, 'n/a', 5]
B = '□□□n/a□'
If you entered this data as strings instead:
B = [1, 2, 3, "n/a", 5]
B = 1×5 string array
"1" "2" "3" "n/a" "5"
you could replace the "n/a" with "NaN" or the missing string and then call double on the resulting string array.
C1 = replace(B, "n/a", string(missing))
C1 = 1×5 string array
"1" "2" "3" <missing> "5"
C2 = double(C1)
C2 = 1×5
1 2 3 NaN 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C3 = replace(B, "n/a", "NaN")
C3 = 1×5 string array
"1" "2" "3" "NaN" "5"
C4 = double(C3)
C4 = 1×5
1 2 3 NaN 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or you could call standardizeMissing and convert the result to double.
C5 = standardizeMissing(B, "n/a") % treat "n/a" as missing
C5 = 1×5 string array
"1" "2" "3" <missing> "5"
C6 = double(C5)
C6 = 1×5
1 2 3 NaN 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Matt J
Matt J 2025-6-6
编辑:Matt J 2025-6-6
It is not possible for an array to hold both numeric floats and chars unless it is a cell array. That being the case, it is easy to convert to double (EDIT: assuming the numbers are integers like in your example, or have less than 5 decimal points precision):
B = {1, 2, 3, 'n/a', 5};
out = str2double(string(B))
out = 1×5
1 2 3 NaN 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  5 个评论
Matt J
Matt J 2025-6-8
Why not simply,
B = {1, 'n/a', 3, 'n/a', 5};
B( ~cellfun(@isnumeric, B) )={nan}
B = 1×5 cell array
{[1]} {[NaN]} {[3]} {[NaN]} {[5]}
Walter Roberson
Walter Roberson 2025-6-8
It depends on whether the strings might represent valid numeric data.
B = {1, '2.34', 3, 'n/a', 5};
mask = cellfun(@isnumeric, B);
B(~mask) = cellfun(@str2double,B(~mask),'uniform',0)
B = 1×5 cell array
{[1]} {[2.3400]} {[3]} {[NaN]} {[5]}
B = {1, '2.34', 3, 'n/a', 5};
B( ~cellfun(@isnumeric, B) )={nan}
B = 1×5 cell array
{[1]} {[NaN]} {[3]} {[NaN]} {[5]}

请先登录,再进行评论。


Catalytic
Catalytic 2025-6-8
编辑:Catalytic 2025-6-8
format long
source = {'0.12345678987654321', pi,'n/a'};
target = [0.12345678987654321, pi,nan];
output = cellfun( @(c) str2double(num2str(c,17)), source)
output = 1×3
0.123456789876543 3.141592653589793 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
isequaln(output,target)
ans = logical
1
  1 个评论
Walter Roberson
Walter Roberson 2025-6-8
Interesting, I just happened to notice
fprintf('%.999g\n', pi)
3.141592653589793115997963468544185161590576171875
fprintf('%.999g\n', pi*1e30)
3141592653589793216413703340032
The representation of pi here goes ... 8979311 but the representation of pi*1e30 goes ... 8979321 so the multiplication by 1e30 leads to different rounding. (Incidentally, true pi goes ... 8979323[8])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by