As the title says, I would like to copy only certain elements of a structure into a new one, where all copied elements have a specific value in one field.
I have an existing structure S with the fields 'ID', 'Direction', 'Length', 'Width'. The field Direction can have two value: '+' and '-'. I want to create a new structure Sp with all '+' elements and Sn with all '-' elements. Is that possible without a for loop like this?
Sp = struct('ID', '', 'Direction', '', ...);
for ii=1:size(S,1)
if S(ii).Direction == '+'
Sp(end+1)=S(ii);
end
end
Ultimately I need to draw a histogram of the Length and Width, differentiating between the + and - elements. If that is possible without the extra structures, I'd be grateful for any tips!
Thank you very much for helping me!
==sign. This works fine for a single char, but if you have a vector, the logical operator will compare the full vector, eg'ab' == 'ac'will give the answer[1,0]. You will also get an error forab=abcsince the vectors have different lengths. As I said this is ok for scalar comparison, but otherwise you may want to usestrcmp(orstrcmpicase insensitive). - patrik