フィルターのクリア

Combining columns in a table into comma separated values while skipping NaN

7 ビュー (過去 30 日間)
Erik J
Erik J 2022 年 10 月 19 日
編集済み: VBBV 2022 年 10 月 21 日
I have many tables where I need to combine values that are not NaN from one column with another column, separated by commas.
The tables I have look like variations of this:
'2022_1_30_12_56_45_972' 0 0 NaN
'2022_1_30_12_56_48_783' 1 0 NaN
'2022_1_30_12_56_55_912' 2 0 2
'2022_1_30_12_57_2_446' 3 1 NaN
'2022_1_30_12_57_9_231' 4 1 NaN
'2022_1_30_12_57_12_990' 5 1 NaN
'2022_1_30_12_57_15_210' 6 1 NaN
What I want is this:
'2022_1_30_12_56_45_972' 0 0
'2022_1_30_12_56_48_783' 1 0
'2022_1_30_12_56_55_912' 2 0,2
'2022_1_30_12_57_2_446' 3 1
'2022_1_30_12_57_9_231' 4 1
'2022_1_30_12_57_12_990' 5 1
'2022_1_30_12_57_15_210' 6 1
I can loop through, find the values to combine, and combine them. But I can't get the 2 values into a single cell in a new table with only 3 columns. The code segment looks like:
for jj = 1:height(currTable)
if isnan(currTable.Var4(jj))
else
currTable.Var3(jj) = strcat(num2str(currTable.Var3(jj)),',',num2str(currTable.Var4(jj)));
end
end
But when I try to combine the values into the third column, I get an error because the size of the new cell is 1x3.
Error using .
Unable to perform assignment because the left and right sides have a different number of elements.
Error in scratch (line 12)
currTable.Var3(jj) = strcat(num2str(currTable.Var3(jj)),',',num2str(currTable.Var4(jj)));

回答 (1 件)

VBBV
VBBV 2022 年 10 月 21 日
編集済み: VBBV 2022 年 10 月 21 日
currTable{jj} = strcat(num2str(currTable.Var3(jj)),',',num2str(currTable.Var4(jj)));
If you use cell arrays it becomes easier rather than putting them in numeric arrays

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by