How to concatenate multiple tables from a list of table names

조회 수: 2 (최근 30일)
Wah On Ho
Wah On Ho 2016년 4월 26일
편집: Stephen23 2019년 6월 19일
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
  댓글 수: 1
Wah On Ho
Wah On Ho 2016년 5월 15일
Thanks John. Since my posting I've managed to solve my problem.

댓글을 달려면 로그인하십시오.


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