How do I horizontally concatenate tables within a cell array (same # of rows, different # of columns) that contain both numeric and string data types?
13 views (last 30 days)
Show older comments
I have a 1 x 8 cell array (length of cell array is subject to change over by use) where each cell contains a 353 x 9 table or 353 x 12 table. In the columns of a cell, there is either numeric or string data. For example, one column may contain area values like 54.5698 where the column next to it contains the units for it as such: 'um^2'. When I run: comb_data = horzcat(data{1,1}{1,:});
I get the error: Unable to concatenate the table variables 'Area' and 'Unit', because their types are double and cell.
Any ideas? Thanks in advance!
Edit: I want to be able to combine the data in these cells by a particular column names ID as some of the data is out of order across cells. Is there a way to manipulate the join function to join the tables by a variable while also having repeating variable names across files?
concat_table = join( tables in cell ,'Keys','ID','KeepOneCopy','Depth','Level','Time','FilamentID','Category',);
2 Comments
Guillaume
on 18 Jul 2018
Right, it's now a completely different question from your original one.
Almost anything is possible, but now I'm not entirely sure of what you want. Can you give a concrete example?
Accepted Answer
Guillaume
on 18 Jul 2018
So it does look like you want to join the tables. join only works on two tables at a time so you'll have to use a loop:
bigtable = join(yourcellarray{1}, yourcellarray{2}); %join first two tables
for tidx = 3:numel(yourcellarray)
bigtable = join(bigtable, yourcellarray{tidx}); %join remaining tables
end
By default join will use all identical variable names to find which rows to merge and any row that has is not matched in the 2nd input table will be discarded, so the above is biased towards the first table in the cell array if there are unmatched rows. It may be you want innerjoin instead of join if you only want to keep only the rows that match all tables or outerjoin if you want all rows (non-matched columns will receive nan).
It may also be that you want to restrict the matching to only ID and Unit, in which case, you indeed use the key option:
join(A, B, 'Keys', {'ID', 'Unit'})
In which case you may indeed want to discard duplicates. Once again preference will be given to the 1st input. The option is indeed 'KeepOneCopy' but you have to enclose the list of names in a cell array:
join(A, B, 'KeepOneCopy', {Depth', 'Level', 'Time', 'FilamentID', 'Category'})
More Answers (0)
See Also
Categories
Find more on Tables 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!