MATLAB: Load and name alot of files in loop instead of dynamic variables

dynamicfilesindexindexingloadloopmatMATLABstructurevariables

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;

Best Answer

Your new code is definitely better than the first one, and you made the correct choice of using arrays instead of using dynamic variable names. In the following code, I suggest one minor improvement and point out a potential error in your code. Overall, your approach is correct
wind = [9 11 17 25 32 56];
num_files = numel(wind); % always a good idea to use a variable instead of hardcoding values.
% It will prevent any trouble if you try to read any other set of files.
Damp = zeros(num_files,6000);
Tp_pyMom = zeros(num_files,52);
Tp_pyTime = zeros(num_files,52);
for k = 1:num_files
A = load(['decayDamp' num2str(wind(k)) '.mat']);
Damp(k,:) = A.v;
B = load(['TpPosDamp' num2str(wind(k)) '.mat']);
TpPy = B.tp_pos;
if k == 5
Tp_pyMom(k,:) = TpPy(1:end-1,2); % I guess you just wanted to exclude the last element.
Tp_pyTime(k,:) = TpPy(1:end-1,1);
else
Tp_pyMom(k,:) = TpPy(:,2);
Tp_pyTime(k,:) = TpPy(:,1);
end
end
Note that you can handle matrices of varying sizes using cell arrays, but they can be a little bit slower than numeric arrays.