How to concatenate multiple tables from a list of table names
2 views (last 30 days)
Show older comments
Hi. I'm a beginner at Matlab and can't work out this problem. I am trying to concatenate multiple tables of data (columns are a mix of numeric and non-numeric). The tables are in my workspace and I've used 'whos' to compile only those tables into a structural table, call it varlist, with the first column being a list of the table names I want to concatenate. I can use 'vertcat' in the command window to test I can manually specify two table names and concatenate them. My goal is to have a script that will look at the list of table names in column 1 of varlist and concatenate them. They may have different numbers of rows but they always have the same number of columns. I can't seem to do this in a script. I've tried accessing with {} and () but with no success. Maybe I need to have the table names in a different type of matrix? Any help would be much appreciated.
1 Comment
Stephen23
on 26 Apr 2016
Edited: Stephen23
on 19 Jun 2019
What you are trying to do is to access variable names dynamically.
The best solution is to avoid trying to access lots of separate variables:
If you imported this data then it is trivial to import the data into one cell array, there are other solutions for other situations, all of which will be faster, neater, and more robust than trying to access lots of variables dynamically. Remember: if you are trying to access variables dynamically then you are writing bad code.
Answers (2)
John BG
on 26 Apr 2016
Edited: John BG
on 29 Apr 2016
there isn't a standard function to concatenate tables, this answer is trivial
when trying horzcat vertcat cat duplicate error happens.
command concat does not recognize concat.
The basic problem is, like when attempting to merge 2 structures, first you have to make sure they have matching fields, let me explain:
strcat(s1,s1) assumes you have 2 vectors of characters each of arbitrary length, yet both have same type.
1st you have to write your own function that first verifies that 2 tables are compatible for merging.
Then when same field, mind possible misleading head and tail spaces, you have to concatenate contents, yet different fields have to be added up, making the output table larger in amount of fields than any of the input tables.
If you paste here example tables of what the inputs may be I will have a look and draft a script.
John
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!