How to concatenate multiple tables from a list of table names

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2016-4-26
编辑:Stephen23 2019-6-19
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.

请先登录,再进行评论。

回答(2 个)

John BG
John BG 2016-4-26
编辑:John BG 2016-4-29
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

Stephen23
Stephen23 2016-4-26
编辑:Stephen23 2019-6-19

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by