Home > Software engineering >  Delete rows from string array with just one value
Delete rows from string array with just one value

Time:03-29

I have a string array like this:

x={{'Na','Mg','Si'};{'V'};{'Na','Mg','Si','S'};{'Si'};{'Na','Mg','Al','P'}}

How can I delete all the rows which contain just one value, to obtain:

x={{'Na','Mg','Si'};{'Na','Mg','Si','S'};{'Na','Mg','Al','P'}}

CodePudding user response:

You don't have a "string array", you have a cell array containing cell arrays (in turn these contain chars).

x={{'Na','Mg','Si'};{'V'};{'Na','Mg','Si','S'};{'Si'};{'Na','Mg','Al','P'}}

You can get the number of elements in each sub-cell with

N = cellfun( @numel, x );

Then remove sub-cells with a single element using

x = x( N > 1 );

This could be done in one line

x = x( cellfun(@numel, x) > 1 );
  • Related