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 件のコメント

Henry Barth
Henry Barth 2022 年 3 月 24 日
are Na,Mg,Si string variables?
Tatjana Mü
Tatjana Mü 2022 年 3 月 24 日
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 日

0 投票

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 件のコメント

Arif Hoq
Arif Hoq 2022 年 3 月 24 日
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}
Tatjana Mü
Tatjana Mü 2022 年 3 月 24 日
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
Tatjana Mü
Tatjana Mü 2022 年 3 月 24 日
@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.
Arif Hoq
Arif Hoq 2022 年 3 月 24 日
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 ^^
Arif Hoq
Arif Hoq 2022 年 3 月 24 日
~ supposed to mean not.
~= menas not equal.
isequal function returns empty cell. so ~ which is used before isempty fucntion returns not empty cell
Henry Barth
Henry Barth 2022 年 3 月 24 日
Isn't that what I said? Please correct me if I'm wrong!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell 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