0

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

Antonio
  • 186
  • 2
  • 12
  • 1
    Do I understand correctly: you want to remove from the dataset all structs whose `Name` field matches any of the strings found in `sn`? – Dev-iL Jun 05 '19 at 08:34
  • @Dev-iL That's right, you understand my idea. sorry, but my english is not good – Antonio Jun 05 '19 at 08:53

2 Answers2

1

The simplest solution I could think of uses string and ismember:

function S2 = q56456298()
%% Generate a dataset:
ROWS = 128;
isUnwanted = randn(ROWS,1) > 0 ;
S = repmat(struct('Name',[], 'Data', []), 1, ROWS);
for ind1 = 1:ROWS
  if isUnwanted(ind1)
    S(ind1).Name = sprintf('Unwanted%u', ind1);
  else
    S(ind1).Name = sprintf('Useful%u', ind1);
  end
  S(ind1).Data = array2table(rand(randi(200),4));
end

%% Remove all "Unwanted fields"
names = string({S.Name}).'; % Here we collect all names, and make it a string array.

toRemove = "Unwanted" + (1:ROWS).'; % This simulates your "sn" array.
[~, idxToDelete] = ismember(toRemove, names);
S2 = S(~idxToDelete); % The result only contains "Useful" rows.
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • have Problem with `string({S.Name}).`; have this error : `Undefined function 'string' for input arguments of type 'cell`. – Antonio Jun 05 '19 at 09:55
  • 2
    This might happen if your MATLAB version is too old. Strings were introduced in **R2016b**. (I thought I could stop mentioning this in 2019... Apparently not.) – Dev-iL Jun 05 '19 at 10:48
0

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));
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147