Extract value from every struct
이전 댓글 표시
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.
채택된 답변
추가 답변 (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
2022년 8월 2일
편집: Walter Roberson
2022년 8월 4일
댓글 수: 7
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
2022년 8월 3일
no definition of Y in this code.
Monalisa Chikezie
2022년 8월 3일
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
2022년 8월 3일
Walter Roberson
2022년 8월 3일
What was your intention in assigning to X_all(:) ?
Monalisa Chikezie
2022년 8월 3일
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
