Combine multiple tables into the one

Hi I have a .mat file which contains 31 variables and all are 21600x5 tables. I want to combine these tables into the one table so that I can then export it excel.
I want to then repeat this for other .mat files saved in the folder.
So far I am able to loop through the mat files and I know how to write a table (as shown below)it is combining all the tables into one table where I am stuck

2 件のコメント

Ameer Hamza
Ameer Hamza 2018 年 4 月 22 日
There is no code given in your question.
Stephen23
Stephen23 2018 年 4 月 22 日

サインインしてコメントする。

回答 (3 件)

Ameer Hamza
Ameer Hamza 2018 年 4 月 22 日

6 投票

You can simply combine tables in the same way as you can combine matrices in MATLAB, as long as dimensions are consistent. For example, if you have two tables t1 and t2 of dimension 21600x5 then

new_table = [t1 t2]  % combine them along column dimension i.e. new table will be 21600x10
new_table = [t1; t2]  % combine them along row dimension i.e. new table will be 43200x5

Since you want to access 31 variables in your workspace, you might want to use whos and evalin() to automatically access all the tables in the base workspace and combine them. As an example

vars = whos;
big_table = [];
for i=vars'
  if i.class == "table"
    big_table = [big_table; evalin('base', i.name)];
  end
end

This will load all the tables present in base workspace into big_table. This will combine the tables according to their names in ascending order (i.e. table axx will be above table bxx in big_table).

3 件のコメント

Stephen23
Stephen23 2018 年 4 月 22 日
編集済み: Stephen23 2018 年 4 月 22 日

@Karina Boyle: note that you should avoid magically accessing variable names as this answer recommends. Magically accessing variable names makes your code slow, complex, buggy, and hard to debug. Functions like eval, evalin, and assignin are what beginners use to force themselves into writing really inefficient code. Read this to know why:

https://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html

One of the most important ways of avoiding having lots of variable is to simply load into one output variable (a structure), rather than simply spamming all of the variables in the .mat file directly into the workspace. Then you can easily and efficiently loop over the fields of that structure. Read this to know more:

https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

See my answer for a simple solution that does not rely on slow inefficient introspective commands (i.e. whos) and slow, inefficient magical accessing of dynamic variable names (i.e. evalin).

Note that one bug this answer already has is it will try to concatenate all variables that are in the workspace, not just the ones from the .mat files. The author will no doubt suggest a "fix" by adding more complexity to the answer.... but the real solution is to write simpler, more robust code by avoiding this approach entirely.

Ameer Hamza
Ameer Hamza 2018 年 4 月 22 日

@Stephen I agree, but this question was mainly about combining several tables into one big table. Therefore to make it general, I wrote the answer, as if, several tables are already present in the base workspace. In that case, magically accessing variable names is a convenient option. I addressed the issue of properly accessing variables in OP's other question, because that question is specifically talking about reading tables from mat files and writing to excel file.

Kyle Thompson
Kyle Thompson 2020 年 12 月 10 日
Bless your soul

サインインしてコメントする。

Stephen23
Stephen23 2018 年 4 月 22 日
編集済み: Stephen23 2018 年 4 月 22 日

2 投票

As I wrote earlier, simply load into a structure. Then you convert the struct to a cell, and it is then trivial to concatenate the contents of all cells:

S = load(...);
C = struct2cell(S);
T = vertcat(C{:});

And that is all. Why waste your time doing anything more complex?

Note that in order to concatenate the tables they will have to have the same variables but unique rows. If they do not have unique rows then you can easily loop over the fields of that structure to add a new variable to each table, or change one of the row values, or whatever you want:

S = load(...);
F = fieldnames(S)
for k = 1:numel(F)
    tmp = S.(F{k}); % get one table
    ... do whatever changes you want to tmp
    S.(F{k}) = tmp;
end

Note how much simpler it is to access the fieldnames of a structure, compared to the slow, complex, and indirect commands which try to access multiple variables in the workspace.

Image Analyst
Image Analyst 2018 年 4 月 22 日

0 投票

You might be interested in the table functions join(), innerjoin(), and outerjoin() for combining tables.

1 件のコメント

Adam Danz
Adam Danz 2018 年 9 月 12 日
Just to add to this list, vertcat() worked for me.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2018 年 4 月 22 日

コメント済み:

2020 年 12 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by