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

5 ビュー (過去 30 日間)
Henning Eimstad
Henning Eimstad 2020 年 4 月 4 日
コメント済み: Henning Eimstad 2020 年 4 月 4 日
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 件のコメント
Henning Eimstad
Henning Eimstad 2020 年 4 月 4 日
編集済み: Henning Eimstad 2020 年 4 月 4 日
Edit:
I came up with the following code
wind = [9 11 17 25 32 56];
Damp = zeros(6,6000);
Tp_pyMom = zeros(6,52);
Tp_pyTime = zeros(6,52);
for k = 1:6
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(end-1,2);
Tp_pyTime(k,:) = TpPy(end-1,1);
else
Tp_pyMom(k,:) = TpPy(:,2);
Tp_pyTime(k,:) = TpPy(:,1);
end
end
For k=5 the result was +1 longer than the rest. Now everything is stores as in Damp(6x6000), and I can access the different data with Damp(1,:) etc. Is this a better way than the first post?

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 4 日
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.
  1 件のコメント
Henning Eimstad
Henning Eimstad 2020 年 4 月 4 日
Perfect! Thanks for the feedback, much appreciated! :)

サインインしてコメントする。

その他の回答 (1 件)

Florian Floh
Florian Floh 2020 年 4 月 4 日
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 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 4 日
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
Henning Eimstad 2020 年 4 月 4 日
Great, thanks alot! Very helpful both of you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by