Accessing elements in a structure

I have 2 matlab files, both containing 2 elements: velocity and points.
i have loaded these two files into a structure, but now I want to access each individual field in the velocity column and do some calculations in it (e.g. work out the mean value).
I am struggling to access each field, could anybody help me please?
file = dir('*.mat');
num_files = length(file);
[~, index] = natsort({file.name});
filelist = file(index);
out = load(filelist(1).name);
for k = 1:numel(filelist)
out(k) = load(filelist(k).name);
end

1 件のコメント

Stephen23
Stephen23 2021 年 7 月 27 日
編集済み: Stephen23 2021 年 7 月 27 日
Unless you have a good reason for using NATSORT you should probably use NATSORTFILES:
which lets you write simpler code by directly sorting the output from DIR:
P = 'absolute or relative filepath to where the files are saved';
S = dir(fullfile(P,'*.mat'));
S = natsortfiles(S); % <------- much simpler!
for k = 1:numel(S)
F = fullfile(P,S(k).name)
... etc
end

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

 採用された回答

KSSV
KSSV 2021 年 7 月 27 日
編集済み: KSSV 2021 年 7 月 27 日

0 投票

file = dir('*.mat'); %fine all the .mat files
num_files = length(file); %record how many files have been found
[~, index] = natsort({file.name}); %sort the files into order
filelist = file(index);
GTav = zeros(numel(filelist),1)
for k = 1:numel(filelist)
load(filelist(k).name); % I hope the structure is named out in every file
% For each cell in the velocity column of the structure
v1 = vel ;
%1. calculate the resultant velocity of each particle
ResV = sqrt((v1(:,1).^2) + (v1(:,2).^2) + (v1(:,3).^2));
%2. calculte the mean resultant velocity of all the particles
ResVav = mean(ResV);
%3. calculate the granular temperature of all the particles
GT = ResV-ResVav;
%4. calculate the average granular temperature for each field
GTav(k) = mean(GT);
end

10 件のコメント

C.G.
C.G. 2021 年 7 月 27 日
Thank you for your response.
I understand what you have done here, but I want to eventually use this to process 1 million files. I am just trying to set-up the code to process this. Is there a way to access these without having to set up 1 million variables?
KSSV
KSSV 2021 年 7 月 27 日
file = dir('*.mat');
num_files = length(file);
[~, index] = natsort({file.name});
filelist = file(index);
out = cell
for k = 1:numel(filelist)
load(filelist(k).name);
p1 = out(1).points ;v1 = out(1).vel ;
p2 = out(2).points ;v2 = out(2).vel ;
% do what you want and save
end
C.G.
C.G. 2021 年 7 月 27 日
even still wont I then have to write p1 and v1 out to 1 million?
KSSV
KSSV 2021 年 7 月 27 日
If you keep on appending the data wont memory exceed? If you want all of them in an array use:
file = dir('*.mat');
num_files = length(file);
[~, index] = natsort({file.name});
filelist = file(index);
out(numel(filelist)) = struct ;
for k = 1:numel(filelist)
out(k).data = load(filelist(k).name);
end
C.G.
C.G. 2021 年 7 月 27 日
I cant change the amount of files I have. Here I write the full code of what I wish to do to each field in the velocity column. But I need this for each field in the velocity column if I have multiple files to load in and not just the one. All I need to save from this is the GTav for each field.
file = dir('*.mat'); %fine all the .mat files
num_files = length(file); %record how many files have been found
[~, index] = natsort({file.name}); %sort the files into order
filelist = file(index);
out = load(filelist(1).name); %pre-assign a structure for the data to go into
for k = 1:numel(filelist)
out(k) = load(filelist(k).name); %load the files into the structure
end
% For each cell in the velocity column of the structure
v1 = out(1).vel ;
%1. calculate the resultant velocity of each particle
ResV = sqrt((v1(:,1).^2) + (v1(:,2).^2) + (v1(:,3).^2));
%2. calculte the mean resultant velocity of all the particles
ResVav = mean(ResV);
%3. calculate the granular temperature of all the particles
GT = ResV-ResVav;
%4. calculate the average granular temperature for each field
GTav = mean(GT);
KSSV
KSSV 2021 年 7 月 27 日
Edited the main answer.
C.G.
C.G. 2021 年 7 月 27 日
thank you. When you say 'I hope the structure is named out in every file', all my files are named out_1.mat, out_2.mat etc.
but I am getting the following error: Undefined variable "out" or class "out.vel". I have attached my 3 .mat files.
KSSV
KSSV 2021 年 7 月 27 日
There is no structure in the .mat file. Each file has just points and vel. So replace the line
v1 = out.vel ;
with
v1 = vel;
I have replaced the line in the answer.
C.G.
C.G. 2021 年 7 月 27 日
Thank you so much for your help.
Stephen23
Stephen23 2021 年 7 月 27 日
編集済み: Stephen23 2021 年 7 月 27 日
Note that KSSV's answer loads directly into the workspace, which is not recommended.
You can make your code more robust by loading into an output variable (which is a scalar structure):
out = load(..);
out.vel

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

質問済み:

2021 年 7 月 27 日

編集済み:

2021 年 7 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by