Note that there are some explanatory texts on larger screens.

plurals
  1. POFind the intersection of two arrays of structs in MATLAB
    primarykey
    data
    text
    <p>I have an array of structs where I perform two searches. First I search for a specific colour and then I search for a specific city. I get two data sets containing the data I was looking for. So far, no problems.</p> <p>From the two data sets I get, I would like to find the structs in the two data sets that are present in both data sets.</p> <p>I have tried 'intersect', as this seemed to be a good option for arrays. But I do not seem not get any intersected data... Why not?</p> <p>The code looks something like this:</p> <pre><code>%Array of structs InfoArray(1) = struct ('Name','AAAA', 'City', 'London', 'Test', '70', 'FavouriteColor', 'red'); InfoArray(2)= struct('Name','BBBB', 'City', 'London', 'Test', '20', 'FavouriteColor', 'blue'); InfoArray(3)= struct('Name','CC', 'City', 'London', 'Test', '10', 'FavouriteColor', 'white'); InfoArray(4)= struct('Name','DD', 'City', 'Stockholm', 'Test', '30', 'FavouriteColor', 'yellow'); InfoArray(5)= struct('Name','EEEEE', 'City', 'Oslo', 'Test', '15', 'FavouriteColor', 'red'); InfoArray(6)= struct('Name','FFFF', 'City', 'Oslo', 'Test', '15', 'FavouriteColor', 'red'); InfoArray(7)= struct('Name','GG', 'City', 'Stockholm', 'Test', '80', 'FavouriteColor', 'blue'); InfoArray(8)= struct('Name','H', 'City', 'Oslo', 'Test', '60', 'FavouriteColor', 'pink'); InfoArray(9)= struct('Name','III', 'City', 'Oslo', 'Test', '5', 'FavouriteColor', 'red'); InfoArray(10)= struct('Name','JJJJ', 'City', 'Stockholm', 'Test', '40', 'FavouriteColor', 'blue'); InfoArray(11)= struct('Name','KKKK', 'City', 'London', 'Test', '70', 'FavouriteColor', 'white'); %Find structs in array with color: 'red' iColor = 'red'; [pFound,matchingFavouriteColors] = findPost(InfoArray,'FavouriteColor',iColor); %Find structs in array with City: 'London' iCity = 'London'; [pFound,matchingCity] = findPost(InfoArray,'City',iCity); %Find the structs that are found in both of the data sets ???? [c, ia, ib] = intersect(matchingFavouriteColors, matchingCity); disp([c; ia; ib]) function [matchFound, matchingData] = findPost(db,sField,iField) matches = find(strcmpi({db.(sField)},iField)); if(isempty(matches)) disp('No matches found'); postsFound=0; else matchingData = db(matches(:)); matchFound=length(matches); end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload