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

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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Arif Hoq
Arif Hoq 2022년 3월 24일
편집: Arif Hoq 2022년 3월 24일
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

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.
Tatjana Mü
Tatjana Mü 2022년 3월 24일
편집: Tatjana Mü 2022년 3월 24일
@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?
Henry Barth
Henry Barth 2022년 3월 24일
편집: Henry Barth 2022년 3월 25일
~ 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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2022년 3월 24일

편집:

2022년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by