How to count the number of elements of a cell having string and integers?

4 vues (au cours des 30 derniers jours)
chocho
chocho le 15 Déc 2018
Commenté : chocho le 17 Déc 2018
Hello guys,
I have a cell array of strings and integer elements as shown here:
'POLR3D,SNAPC5'
'HIST3H2BB,HIST3H2BB'
'TFAM'
0
'CYP4F2'
0
'TRNT1,NUP214,NUP160,TRNT1'
'RPS21'
'ACTR3'
0
I used this code to count the elements of this cell but doesn't work , i think because this cell have multiple types.
So is it possible to count this cell elements or need some transformations types as i tried int2str, string but non of them worked!
SS=(tt(:,2));
n3=size(SS,1);
sub_stage_col2 = cell(1, n3);
for k1 = 1:n3
sub_stage_col2{k1} = strtrim(strsplit(SS{k1,:} ,','));
end
ptws_count2 = cellfun(@(x) numel(x), sub_stage_col2);
for m1=1:n3
pathws_count2(m1,1)=ptws_count2(m1);
end
Also this cell have some repeated elements in some of its rows in rows 2 and 7: 'HIST3H2BB,HIST3H2BB', 'TRNT1,NUP214,NUP160,TRNT1 'How to remove the duplicates ?
I again appreciate any help!
  4 commentaires
chocho
chocho le 16 Déc 2018
@Image Analyst I want to count the elements of this cell array and when there is 0 put it 0 bcz i mean by 0 no element.
I attached the matfile
chocho
chocho le 16 Déc 2018
Can anyone help me out plz ?

Connectez-vous pour commenter.

Réponse acceptée

TADA
TADA le 15 Déc 2018
Modifié(e) : TADA le 15 Déc 2018
because you have multiple types, most likely you will need to either selectively change to a uniforrm data type at the begining or handle the data selectively according to it's datatype (i.e using ischar/isnumeric)
I assumed the cell array you supplied is this:
{'POLR3D,SNAPC5'; 'HIST3H2BB,HIST3H2BB'; 'TFAM'; 0; 'CYP4F2'; 0; 'TRNT1,NUP214,NUP160,TRNT1'; 'RPS21'; 'ACTR3'; 0};
and I assumed it's the value of your SS variable. historically speaking, this is an ill name choice if you ask me. That being said, I got this going:
SS = {'POLR3D,SNAPC5'; 'HIST3H2BB,HIST3H2BB'; 'TFAM'; 0; 'CYP4F2'; 0; 'TRNT1,NUP214,NUP160,TRNT1'; 'RPS21'; 'ACTR3'; 0};
n3 = size(SS,1);
sub_stage_col2 = cell(1, n3);
for k1 = 1:n3
if ischar(SS{k1})
sub_stage_col2{k1} = unique(strtrim(strsplit(SS{k1} ,',')));
else
sub_stage_col2{k1} = SS{k1};
end
end
pathws_count2(:,1) = cellfun(@(x) numel(x), sub_stage_col2(:));
disp(pathws_count2);
2
1
1
1
1
1
3
1
1
1
the use of unique function gets rid of the duplicates within each row
Putting the result of cellfun in one array then iterating to copy to a second array is unnecessary, even if the number of rows in pathws_count2 and sub_stage_col2 is different (then you can set only the desired rows.
and the call to strsplit is a potential bug:
strsplit(SS{k1,:}, ',')
will be the same as calling
strsplit(SS{k1}, ',')
unless SS is a matrix with more than one column (or a row vector), in which case, you will probably get an exception since it's like calling
strsplit(SS{k1,1}, SS{k1,2}, SS{k1,3},...,',')
  3 commentaires
TADA
TADA le 16 Déc 2018
Modifié(e) : TADA le 16 Déc 2018
You Can Save The Output Cell Array Without unique Then Invoke unique And Save That In Another Cell Array.
As For The Zero, Just Change
% Not Tested
if isnumeric(SS{k1})
if SS{k1} == 0
sub_stage_col2{k1} = '';
else
% If You Need It
sub_stage_col2{k1} = SS{k1};
end
end
I Don't Know Where You Get That Data From But Perhaps You Can Change The Way You Parse That Data To begin With To A Better Data structure
chocho
chocho le 17 Déc 2018
@TADA Thanks TADA for your great help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by