Check if a value from a dropdown menu is in a struct

1 view (last 30 days)
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!

Accepted Answer

Stephen23
Stephen23 on 19 Jun 2019
Edited: Stephen23 on 19 Jun 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:
You can easily resolve this using ismembertol
or compare the absolute difference against a tolerance:
abs(A-B)<tol

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!