Filter löschen
Filter löschen

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

12 Ansichten (letzte 30 Tage)
Erik J
Erik J am 19 Okt. 2022
Bearbeitet: VBBV am 21 Okt. 2022
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)));

Antworten (1)

VBBV
VBBV am 21 Okt. 2022
Bearbeitet: VBBV am 21 Okt. 2022
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

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by