Borrar filtros
Borrar filtros

Concatenate variables from multiple files to just one single matlab file

26 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Stephen23
Stephen23 el 4 de Sept. de 2018
Editada: Stephen23 el 29 de Nov. de 2022
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 comentarios
Samy Alkhayat
Samy Alkhayat el 11 de Jul. de 2024 a las 19:20
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
Stephen23 el 12 de Jul. de 2024 a las 4:38
Editada: Stephen23 el 12 de Jul. de 2024 a las 4:43
"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

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 3 de Sept. de 2018
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 comentario
Stephen23
Stephen23 el 4 de Sept. de 2018
Editada: Stephen23 el 4 de Sept. de 2018
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by