Check if a value from a dropdown menu is in a struct
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi. I have the following code, where I struggle because no member is detected.
My 'idx' just return a 1*80 logical containing zeros. When I debug I can see that 'a' contains 16 doubles with the number 67.2022 and 'b2' is a double variable with the number 67.2022. So the are both the same type.
a = [app.sortedS.SliceLocation]; % this is a column from a struct containing different double values
b = app.dd_SliceLocation.Value; % this is a value from a dropdown box
b2 = str2double(b);
for ii = 1:length(a)
idx(ii) = ismember(b2,a(ii));
end
imageT = app.sortedS.Image(idx) % Image is a column in the same struct (sortedS) as 'a'.
Hope you are able to help!
0 commentaires
Réponse acceptée
Stephen23
le 19 Juin 2019
Modifié(e) : Stephen23
le 19 Juin 2019
Most likely those values are not exactly the same because you are trying to compare floating-point numbers, which is never a good idea, as has been discussed countless times already on this forum:
https://www.mathworks.com/matlabcentral/answers/423132-why-is-matlab-messing-up-this-very-simple-calculation#answer_340581
https://www.mathworks.com/matlabcentral/answers/272092-basic-indexing-question-find-ismember#answer_212787
You can easily resolve this using ismembertol
or compare the absolute difference against a tolerance:
abs(A-B)<tol
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!