Loading multiple (e.g. ~ 3000) mat files into workspace and merging them in a table

조회 수: 1 (최근 30일)
I have many mat files (no particular sequence such as file1.mat, file2.mat etc etc) in a folder. I want to load all of them into a workspace and then merge all the mat files in a table.
Any efficient way to do so other than loading all of them manually (which can be done if I select all the files using CTRL+A then press enter, no issues of RAM as I have 16 gigs and the files will take at best 3 gigs of RAM) and then using the table(names of variables,..........)? My main issue is i will need to list all 3000 variables from workspace to table function manually which I don't want to do.

채택된 답변

Stephen23
Stephen23 2019년 6월 5일
편집: Stephen23 2019년 6월 5일
" i will need to list all 3000 variables from workspace.."
That is a good example of why load-ing directly into the workspace is a bad way to write code.
Preallocate a table (exactly how depends on how you want to join the tables).
Loop over the files. Load each file into a structure, i.e.:
S = load(...)
Convert the structure to a table using struct2table:
T = struct2table(S);
Use one of the table join functions to add the new table to the preallocated table:

추가 답변 (1개)

Swapnil Sayan Saha
Swapnil Sayan Saha 2019년 6월 5일
Ok, let the question be here as this problem is faced by many. Solution (works only if you have sufficient RAM)
  1. Select all the mat files in the folder using CTRL + A, press enter to load all variables in workspace.
  2. Open a new script
  3. Select all variables from workspace using CTRL + A, copy (CTRL + C) and paste them inside the braces of table() function. For example: x = table(var1, var2, ..... varN);
If anyone comes up with a better solution (such as using datastores and tall functions without having to load all mat files in primary memory), you are welcome to share.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by