Loading variables from different folders containing subfolders in loop
Afficher commentaires plus anciens
Hello,
I have 39 folders, each of them containing 6 subfolders with mat files inside in the form of a matrix mxn. From the matfiles, I need to load the matrices with the lowest n of zeros in a loop and save them in a mat.file in the workspace so that those can be loaded when it is necessary to save variables in them.
Any suggestion on how to do that?
Thank you!
4 commentaires
Use DIR with the '**' syntax to get a structure of all of the files:
Loop over that structure. Perform whatever check or comparison you need inside the loop.
Eleonora Montagnani
le 20 Jan 2022
@Eleonora Montagnani: it sounds like you probably just need to store the imported data in an array. One approach is to use a cell array, as shown in the MATLAB documentation:
Another simple and versatile approach is to use the structure returned by DIR:
P = 'absolute or relative path to the root directory',
S = dir(fullfile(P,'**','*.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
D = load(F);
.. decide what data you want to store
S(k).somedata = whatever_you_want_to_store;
S(k).otherdata = something_else;
end
Then you can use another loop to compare those matrices and perform your analyses.
Eleonora Montagnani
le 20 Jan 2022
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!