Extract value from every struct

2 ビュー (過去 30 日間)
Monalisa Chikezie
Monalisa Chikezie 2022 年 8 月 2 日
コメント済み: Monalisa Chikezie 2022 年 8 月 9 日
I have four 1x1 struct files like the attached. mat file in every 60 subfolder, I want to extract the u field in each struct and assign its name to it. The name is in a field called name e.g, the name of this one is rdf. Then use it as one of the X e.g (model = fitcsvm(X,Y, 'KernelFunction','linear', 'BoxConstraint',1);) for svm classification. where, X= [X1 X2 X3 X4] and X1 is currently rdf. Using a for loop, please how do I extract something from a struct.

採用された回答

Walter Roberson
Walter Roberson 2022 年 8 月 4 日
編集済み: Walter Roberson 2022 年 8 月 6 日
vois={'ldF', 'rdF', 'lvF', 'rvF'};
numvois = length(vois);
basedir = 'place/where/the/folders/are';
foldinfo = dir(basename);
foldinfo(~[foldinfo.isdir]) = []; %remove non-folders
foldinfo(ismember({foldinfo.name}, {'.', '..'})) = []; %remove . and ..
foldernames = fullfile({foldinfo.folder}, {foldinfo.name});
numfolders = length(foldernames);
X_all = cell(numfolders, numvois);
for K = 1 : numfolders
thisfolder = foldernames{K};
[~, basename, ~] = fileparts(thisfolder);
for vidx = 1 : numvois
filename = fullfile(thisfolder, basename + "_" + vidx + ".mat");
Features = load(filename);
X_i = Features.xY.u;
X_all{thisfolder, vidx} = X_i;
end
end
At the end of this, X_all will be a cell array with as many rows as folders (60, you said), and as many columns as vois (4)
  5 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 6 日
Ah, I fixed it.
Monalisa Chikezie
Monalisa Chikezie 2022 年 8 月 9 日
oh, thanks a lot

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

その他の回答 (2 件)

Karim
Karim 2022 年 8 月 2 日
編集済み: Karim 2022 年 8 月 2 日
You can acces the data in the struct by placing a point after the variable name of the struct, follewed by the fieldname you want to acces. See below for an example for the 'u' variable.
Features = load(websave('myFile', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1085720/VOI_rdF_1.mat"));
% first extract xY from 'Features'
xY = Features.xY;
% then extract u from the struct
u = xY.u;
% plot the data
figure
plot(u)
grid on

Monalisa Chikezie
Monalisa Chikezie 2022 年 8 月 2 日
編集済み: Walter Roberson 2022 年 8 月 4 日
Hi, thanks for the respnse.
this is what I have so far, but it seems to give an error.
vois={'ldF', 'rdF', 'lvF', 'rvF'};
for j=1:4
Features = load(pwd, '_1.mat'));
X_i=Features.xY.u;
X_all(:)=X_i;
  7 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 3 日
What was your intention in assigning to X_all(:) ?
Monalisa Chikezie
Monalisa Chikezie 2022 年 8 月 3 日
to capture all of the 4 vois in each of the 60 folders

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

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by