How do I combine multiple MAT files into a single matrix

24 views (last 30 days)
Hi,
I'm running a script currently that analyses multiple c3d files and outputs al the variables im interested in as a single array (1 row x N columns).
So I end up with multiple saved flies let say for example A.mat, B.mat, C.mat. all containing the same array layout but with different values, but they will all have the same Variable Name in the workspace.
What I want to do is combine files A, B, and C into one Matrix so that as I load a new .Mat file it will populate underneath the previous row of data.
Any suggestions?
Sorry I'm a bit of a rookie when it comes to Mablab

Answers (3)

Stephen23
Stephen23 on 12 Apr 2017
Edited: Stephen23 on 12 Apr 2017
Easy: load into a variable (which is a structure):
S = load(...);
Do this in a loop, and construct a non-scalar structure containing all of the mat files' data. Here are some examples:
Once you have the data in the structure it is easy to access or combine together into one array.

Jan
Jan on 12 Apr 2017
Try something like this:
FileList = dir('*.mat');
DataC = cell(1, numel(FileList));
for iFile = 1:numel(FileList);
FileData = load(FileList(iFile).name);
DataC{iFile} = FileData.YourVar;
end
DataM = cat(1, DataC{:}); % Or the 2nd or 3rd dimension?!
This imports all files, stores the wanted data in a cell and creates a matrix finally.

Sean Byrne
Sean Byrne on 13 Apr 2017
Thanks for the comments I managed to find a solution that worked well.
Var1= uigetfile('folderlocation','MultiSelect','on');
nVar1 = length(UPSS);
for i = int8(1:nVar1)
[filename, dir] = uigetfile('folderlocation','SelectMATfile');
load([dir filename10])
VariableNameCombined(i,:) = VariableName;
end

Categories

Find more on Structures 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!