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일

0 개 추천

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

Monalisa Chikezie
Monalisa Chikezie 2022년 8월 5일
Hi, this gave me an error. I'm trying to find out why. however, each vois xY.u is 1 * 198 double, subfolder= 60 and there are 4 vois in total.
Walter Roberson
Walter Roberson 2022년 8월 5일
what error do you see.
Monalisa Chikezie
Monalisa Chikezie 2022년 8월 6일
File: loading_data_test.m Line: 15 Column: 71
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched
delimiters.
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일

0 개 추천

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일

0 개 추천

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

Karim
Karim 2022년 8월 2일
FYI, you can just comment below the answer, you do not need to add an extra answer ;)
I did not know that you loaded the data into another variable, I adjusted my answer accordingly.
Walter Roberson
Walter Roberson 2022년 8월 3일
no definition of Y in this code.
Monalisa Chikezie
Monalisa Chikezie 2022년 8월 3일
Y, here was an error. I've corrected it and edited the question for better explanation of what I am trying to do
Walter Roberson
Walter Roberson 2022년 8월 3일
편집: Walter Roberson 2022년 8월 3일
When you assign to X_all(:) and X_all does not already exist then the resulting size is the same as the right hand side. This syntax for an initial assignment does not specifically create a column vector.
When you assign to X_all(:) and the right hand side does not have exactly the same number of elements as X_all has, then you will get an error.
If the two sides have the same number of elements exactly then the values would be copied in linear index order even if the two sides had different shapes.
I predict that the files contain different data sizes and your way of overwriting all of X_all in place is causing failure
Monalisa Chikezie
Monalisa Chikezie 2022년 8월 3일
Alright, thank you. How do I extract it properly without errors
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by