필터 지우기
필터 지우기

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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by