Search code examples
matlabstructurestrncmp

Delete specific rows by imposing a condition


I have a structure consisting of a Names column and a Data column.

enter image description here I need to delete a series of row by imposing the condition with respect to a specific name. I used this code in another exercise and it seemed to be fine, but I guess it's not correct :

    sn = {'Adattamento ad una distrib._HID',...
          'Adattamento ad una distrib._HI1',...
          'TUTTI','Modelling','Sheet37','Sheet52'}; % fogli da escludere

   SheetNames = {S.Name}; %% 

    for jj = 1:length(sn)
      SheetNames = {S.Name};
      S = S(~strncmp(SheetNames, sn(jj),jj));
      %jj = numel(sn)-1; % aggiorna l'indice
    end  

----------------------------UPDATE------------------------------------ I understood the problem.

My S.Name structure is so made:

SheetNames = {S.Name};



  This is {S.Name} :

    {'Ar1';'Adattamento ad una distrib._HID';'Adattamento ad una distrib._HI1';...;'Ar2';'Ar35';...;
'Cos1';'Cos2';'Cos31';...;'Tex1';'Tex2';....;
'Sheet37_HID';'Tex8';.....;'Tex30';'Tu1';'Tu2';'Tu3';...;'Tu32';
'TUTTI';'Modelling';'Sheet52'}

if

 sn = {'Adattamento ad una distrib._HID',...
          'Adattamento ad una distrib._HI1',...
          'TUTTI','Modelling','Sheet37','Sheet52'};

the final structure S, will no longer contain the names that begin A,T,M,S

S = 1x128 after the loop it becomes an S = 1x91


Solution

  • Let the data be defined as

    S(1).Name = 'Ar1'; S(1).Data = [1 2 3];
    S(2).Name = 'Adattamento ad una distrib._HID'; S(2).Data = 'abcd';
    S(3).Name = 'Adattamento ad una distrib._HI1'; S(3).Data = [true; false];
    S(4).Name = 'Ar4'; S(4).Data = {'4' '5'};
    sn = {'Adattamento ad una distrib._HID',...
          'Adattamento ad una distrib._HI1',...
          'TUTTI','Modelling','Sheet37','Sheet52'};
    

    Then, you can use ismember and logical indexing as follows:

    result = S(~ismember({S.Name}, sn));