load multiple .mat files
Show older comments
I am trying to load multiple .mat files through a script. Each file is a recorded channel, that contains data logged at high frequency (so some files are large) with a time stamp. The file name corresponds to the channel name allocated in a logging device. Ideally, I would like to end up with a table with all data combined from all .mat files.
I tried the following:
clear
PathName = uigetdir;
file_all = dir(fullfile(PathName,'*.mat'));
matfile = file_all([file_all.isdir] == 0);
clear file_all PathName
%d=dir('*.mat'); % get the list of files
x=[]; % start w/ an empty array
for i=1:length(matfile)
x=[x; load(matfile(i).name)]; % read/concatenate into x
if true
% code
end
but get the error: Error using vertcat Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
file names are different sizes, I don't know if this is the issue? Is there a simple way to load multiple files of numerical data? many thanks
2 Comments
KSSV
on 23 May 2017
x=[x; load(matfile(i).name)];
This is wrong. Simply use
load(matfile(i).name) ;
And initialize the table and put your variables in that table.
@KSSV: it is recommended to always load into a structure:
S = load(...)
this avoids many of the problems related to creating variables in workspaces, and makes debugging simpler.
"This is wrong"
Why? As long as the variables in the .mat have the same names, concatenating the load-ed structures will work perfectly, and is in fact preferable to loading directly into the workspace (as you showed). Can you please explain why you believe that "This is wrong".
Accepted Answer
More Answers (1)
woody1983
on 23 May 2017
0 votes
Categories
Find more on Workspace Variables and MAT Files 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!