Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find with a struct

I have a struct that holds thousands of samples of data. Each data point contains multiple objects. For example:

Structure(1).a = 7
Structure(1).b = 3
Structure(2).a = 2
Structure(2).b = 6
Structure(3).a = 1
Structure(3).b = 6
...
... (thousands more)
...
Structure(2345).a = 4
Structure(2345).b = 9

... and so on.

If I wanted to find the index number of all the '.b' objects containing the number 6, I would have expected the following function would do the trick:

find(Structure.b == 6)

... and I would expect the answer to contain '2' and '3' (for the input shown above).

However, this doesn't work. What is the correct syntax and/or could I be arranging my data in a more logical way in the first place?

like image 450
CaptainProg Avatar asked Jan 23 '13 13:01

CaptainProg


2 Answers

The syntax Structure.b for an array of structs gives you a comma-separated list, so you'll have to concatenate them all (for instance, using brackets []) in order to obtain a vector:

find([Structure.b] == 6)

For the input shown above, the result is as expected:

ans =
     2     3

As Jonas noted, this would work only if there are no fields containing empty matrices, because empty matrices will not be reflected in the concatenation result.

Handling structs with empty fields

If you suspect that these fields may contain empty matrices, either convert them to NaNs (if possible...) or consider using one of the safer solutions suggested by Rody.

In addition, I've thought of another interesting workaround for this using strings. We can concatenate everything into a delimited string to keep the information about empty fields, and then tokenize it back (this, in my humble opinion, is easier to be done in MATLAB than handle numerical values stored in cells).

Inspired by Jonas' comment, we can convert empty fields to NaNs like so:

str = sprintf('%f,', Structure.b)
B = textscan(str, '%f', 'delimiter', ',', 'EmptyValue', NaN)

and this allows you to apply find on the contents of B:

find(B{:} == 6)

ans =
     2
     3
like image 58
Eitan T Avatar answered Nov 15 '22 19:11

Eitan T


Building on EitanT's answer with Jonas' comment, a safer way could be

>> S(1).a = 7;
   S(1).b = 3;
   S(2).a = 2;
   S(2).b = 6;
   S(3).a = 1;
   S(3).b = [];
   S(4).a = 1;
   S(4).b = 6;

>> find( cellfun(@(x)isequal(x,6),{S.b}) )
ans =
     2     4

It's probably not very fast though (compared to EitanT's version), so only use this when needed.

like image 9
Rody Oldenhuis Avatar answered Nov 15 '22 19:11

Rody Oldenhuis