How do I horizontally concatenate tables within a cell array (same # of rows, different # of columns) that contain both numeric and string data types?

14 views (last 30 days)
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
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?
Karen DSouza
Karen DSouza on 18 Jul 2018
I'm sorry for any confusion that could cause!
So I have a cell array named data of dim 1 x 8 that looks like the following:
[353x9 table 353x9 table 353x9 table 353x9 table 353x9 table 353x12 table 353x9 table 353x9 table]
In the tables there are many duplicate variable names, some containing necessary data and some completely duplicate. The duplicate variable name with data I want to keep from every table are: 'Unit' and 'ID'. Across all the tables, I only want to keep one copy of the columns named 'Depth','Level','Time','FilamentID', and'Category'.
In the tables that are 353 x 9, only one column variable changes and that is the variable of interest. The only table that is 353 x 12 holds 3 columns of interest that are positional data points in x y and z.
In the columns named ID across all table, the order of ID numbers are out of order so a simple concatenation of all data would be incorrect.
ex of 353 x 12:

Sign in to comment.

Accepted Answer

Guillaume
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'})
  1 Comment
Karen DSouza
Karen DSouza on 18 Jul 2018
Thank you so much! I had to manipulate it a little but this is what I finally used:
bigtable = join(data{1}, data{2}, 'Keys', 'ID', 'KeepOneCopy', {'Depth', 'Level', 'Time', 'FilamentID', 'Category'} ); %join remaining tables
for tidx = 3:numel(data)
%data.var1_converted = cellfun(@(x) char(x),data,'UniformOutput',false);
bigtable = join(bigtable, data{tidx}, 'Keys', 'ID', 'KeepOneCopy', {'Depth', 'Level', 'Time', 'FilamentID', 'Category'} ); %join remaining tables
end

Sign in to comment.

More Answers (0)

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!