Load and name alot of files in loop instead of dynamic variables
Afficher commentaires plus anciens
I want to load a lot of different .mat files properly instead of using dynamic variables which I have read is pretty bad.. I have more files than shown here but just to use this as an example of what I want to avoid. A loop and a neat way to handle the data (e.g indexing) for further calculation and graphs would be awesome.
Edit: The number corresponds to different windspeeds, so I have a vector with wind = [9 11 17 25 32 56] I have tried to use for naming and loading without any luck.
Thanks in advance!
A9 = load('decayDamp9');
A11 = load('decayDamp11');
A17 = load('decayDamp17');
B9 = load('TpPosDamp9');
B11 = load('TpPosDamp11');
B17 = load('TpPosDamp17');
Damp9 = A9.v;
Damp11 = A11.v;
Damp17 = A17.v;
Tp9 = B9.tp_pos;
Tp11 = B11.tp_pos;
Tp17 = B17.tp_pos;
1 commentaire
Henning Eimstad
le 4 Avr 2020
Modifié(e) : Henning Eimstad
le 4 Avr 2020
Réponse acceptée
Plus de réponses (1)
Florian Floh
le 4 Avr 2020
Hi!
For loading multiple '.mat' files, I would suggest using the function dir(). You simple store all the desired '.mat' files in one direction (e.g. where your script file is stored), so that your code can access them all at once.
This is the code I would suggest for loading these '.mat' files:
%% Check for *.mat files in this directory
% the function dir() returns a struct that contains (among others) the name
% of the stored '*.mat' files
a = dir('*.mat');
%% Load all the *.mat files in this directory
% Get the number of rows and columns of the struct a
[nrRowStruct nrColStruct] = size(a);
% Use a for loop to load each file by its name
for i=1:nrRowStruct
%load each of the files into the workspace
A =load(a(i).name);
% Here you can process the struct A, as you did in your code before
end
Note that my code is not complete, so you have to add whatever you want to do with the struct A, otherwise you would just see the last struct, that was loaded in your workspace ;)
I hope this was helpful, if not, just let me know ;)
4 commentaires
Ameer Hamza
le 4 Avr 2020
Although using dir is useful in many cases, It will make the code difficult in this case. The OP wants to load two different types of file names in the same loop
decayDampX.mat
TpPosDampX.mat
With dir(), another if condition will be needed to distinguish between these two files.
Henning Eimstad
le 4 Avr 2020
Modifié(e) : Henning Eimstad
le 4 Avr 2020
Ameer Hamza
le 4 Avr 2020
Henning, here is an example of using dir() to load the files decayDampX.mat. You can similarly extend to the other file too. It will be useful if you have lot of files with name in same format.
files = dir('decayDamp*.mat'); % load all files with names decayDampX.mat
num_files = numel(files);
Damp = zeros(num_files,6000);
for k = 1:num_files
A = load(files(k).name);
Damp(k,:) = A.v;
end
Henning Eimstad
le 4 Avr 2020
Catégories
En savoir plus sur Variables 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!