Extract value from every struct
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (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
il 2 Ago 2022
Modificato: Walter Roberson
il 4 Ago 2022
7 Commenti
Karim
il 2 Ago 2022
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
il 3 Ago 2022
no definition of Y in this code.
Monalisa Chikezie
il 3 Ago 2022
Walter Roberson
il 3 Ago 2022
Modificato: Walter Roberson
il 3 Ago 2022
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
il 3 Ago 2022
Walter Roberson
il 3 Ago 2022
What was your intention in assigning to X_all(:) ?
Monalisa Chikezie
il 3 Ago 2022
Categorie
Scopri di più su Structures in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
