Concatenate variables from multiple files to just one single matlab file
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi everyone, I have a folder that contains Matlab files in the format 'station001.mat', 'station002.mat' to lets say 'station035.mat' It has the following variabes 'stationI' and 'stationV' . all the files have the variable name as the same but with different number of values i.e 'stationI' and 'stationV' of 'station001.mat' may have 200 vales each but 'stationI' and 'stationV' of 'station002.mat' may have 300 values each. I need a single Matlab file named station.mat having variables 'stationI' and 'stationV'which contains all the variable values concatenated altogether. Could anyone help me in this, please?
Many thanks
댓글 수: 0
채택된 답변
Stephen23
2018년 9월 4일
편집: Stephen23
2022년 11월 29일
This assumes that the files are returned by the OS in the required order (i.e. the filenames use leading zeros), otherwise download my FEX submission natsortfiles.
D = 'directory where the files are stored';
S = dir(fullfile(D,'station*.mat'));
S = natsortfiles(S); % (optional) alphanumeric sort by filename
C = cell(1,numel(S));
Z = load(fullfile(D,S(1).name));
F = fieldnames(Z);
for k = 2:numel(S)
T = load(fullfile(D,S(k).name));
for n = 1:numel(F)
Z.(F{n}) = cat(1, Z.(F{n}), T.(F{n})); % pick a suitable dimension
end
end
save(fullfile(D,'station.mat'),'-struct','Z')
댓글 수: 7
Samy Alkhayat
2024년 7월 11일
Hello Stephen,
I have successfuly used the code above before, now I need for to concatenate specific vectors from those .mat files since not all 80k files I have include the same number of vectors so it thows the error below, any thoughts will be appreciated!
Error using cat
Number of fields in structure arrays being concatenated do not match. Concatenation of structure arrays
requires that these arrays have the same set of fields.
Stephen23
2024년 7월 12일
편집: Stephen23
2024년 7월 12일
"now I need for to concatenate specific vectors from those .mat files since not all 80k files I have include the same number of vectors so it thows the error below, any thoughts will be appreciated!"
If all files have the same subset of "specific" vectors (i.e. intersection), then you could define that subset yourself:
F = {'names','of','vectors','that','exist','in','all','files'};
And/or you could also add ISFIELD within the innermost FOR-loop, to check if the fields exist:
if isfield(T,F{n})
% concatenate
end
추가 답변 (1개)
dpb
2018년 9월 3일
d=dir('station*.mat'); % return the list of files
n=length(d); % number files found
c=cell(n,1); % allocate a cell array for the returned data
for i=1:n % iterate over the list
c(i)={struct2array(load(d(i).name))}; % put each set of data in the cell array
end
save station cell2mat(c) % put the data into station.mat
The above does save the results as a 2-column array instead of as two separate variables; if it is mandatory to keep those two names just recast the array to two variables before save.
In general, it's better practice in Matlab to use arrays and indexing over individually-named variables with metadata encoded in the variable names, however.
댓글 수: 1
Stephen23
2018년 9월 4일
편집: Stephen23
2018년 9월 4일
Note that there is no guarantee about the order of the fields returned by a .mat file, so this method loses any information related to the variable names' meta-data. One easy solution is to apply orderfields.
The command struct2cell might be of interest too.
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!