Combining columns in a table into comma separated values while skipping NaN
3 views (last 30 days)
Show older comments
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)));
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!