Delete rows from string array with just one value

Hi,
I have a string array like this:
x=[Na,Mg,Si ; V ; Na,Mg,Si,S ; Si ; Na,Mg,Al,P]
I want to delete all the rows which contain just one Value. Does somebody has an idea how it could work? Because I reached my limit of knowledge (really new in matlab).
I appreciate any idea :-)

2 Comments

are Na,Mg,Si string variables?
Yes, the string array just contains Elements as string variables. The problem is that they change from sample to sample, depends on the sample. Sometimes the elements stands alone in a row, but also in rows with other elements, so I cant just delete the elementname. I need a solution to general delete rows with just one value. I hope you understand what I mean.

Sign in to comment.

 Accepted Answer

as i don't have your data so i have added NaN string to make the same dimension cell array
x={'Na','Mg','Si' NaN; 'V' NaN NaN NaN; 'Na','Mg','Si','S' ; 'Si' NaN NaN NaN; 'Na','Mg','Al','P'};
a=string(x);
y=rmmissing(a,1,'MinNumMissing',2);
output=cellstr(y)
output = 3×4 cell array
{'Na'} {'Mg'} {'Si'} {0×0 char} {'Na'} {'Mg'} {'Si'} {'S' } {'Na'} {'Mg'} {'Al'} {'P' }

8 Comments

Or if you cell array looks different
x={{'Na','Mg','Si'};{'V'};{'Na','Mg','Si','S'};{'Si'};{'Na','Mg','Al','P'}}
x = 5×1 cell array
{1×3 cell} {1×1 cell} {1×4 cell} {1×1 cell} {1×4 cell}
for i=1:size(x,1)
if numel(x{i,:})==1
x{i,:}=[];
end
end
output=x(~cellfun('isempty',x))
output = 3×1 cell array
{1×3 cell} {1×4 cell} {1×4 cell}
Sadly, it doesnt work, because the size of the array varies :-/ I attached my resultfile. For example the first row is just Na and should be deleted. But every resultfile is different in size
@Arif Hoq your idea is really good. I tried it with my data. My string array is called "intens_RL_final".
for t=1:size(intens_RL_final,1)
if numel (intens_RL_final{t,:})==1
intens_RL_final{t,:}=[];
end
end
intens_RL_final=intens_RL_final(~cellfun('isempty',intens_RL_final));
I attached you the result. Every element is now in one column. I don't find my mistake.
it should work like that.
@Arif Hoq Sorry I dont understand the last line. So first you search for lines which have the size 1 and delete them? Do I understand correct, that you use the cellfun line for deleting lines which are empty? Why do you use "~" in front of the cellfun function?
~ means NOT in matlab. If you take a closer look at this line of code:
intens_RL_final = intens_RL_final(~cellfun(@isempty,intens_RL_final));
cellfun(@isempty,intens_RL_final);
takes the handle to the isempty-function and your cell array of strings, loops over the cells and applies the function to every cell. Pretty much like this:
isCellEmpty = false(size(intens_RL_final));
for nCell = 1:numel(intens_RL_final)
isCellEmpty(nCell) = isempty(intens_RL_final{nCell});
end
but in a slightly slower but more comfortable way. If we take a cell array as an example:
intens_RL_final = {{"Na","Mg","Si"};{[]};{"Na","Mg","Si","S"};{[]};{"Na","Mg","Al","P"}};
The logical output of this ( [false, true, false, true, false]) is then negated to ([true, false, true, false, true]). If this vector is applied to the cell array, every cell which has a corresponding true-element gets returned. Please be careful with char, string and cell data types. They are different and I still remember that it took a while for me to understand. The official matlab help sites are good when it comes to data types, I would recommend a read.
P.S.: The cellfun function, whilst having its drawbacks, is pretty powerful:
intens_RL_final = intens_RL_final(cellfun(@(cellIn) numel(cellIn)~=1,intens_RL_final));
This is one of the possible one-liners using cellfun that replaces your whole loop as well as the last line whilst only being barely readable ^^
~ supposed to mean not.
~= menas not equal.
isequal function returns empty cell. so ~ which is used before isempty fucntion returns not empty cell
Isn't that what I said? Please correct me if I'm wrong!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Mar 2022

Edited:

on 25 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!