cellarray which contains different types of data

40 vues (au cours des 30 derniers jours)
sermet
sermet le 13 Mai 2014
Commenté : sermet le 13 Mai 2014
belows came from the results from codes in editor page;
txt1 =
'p4004'
[4005]
[4007]
[4009]
[4015]
[4020]
[4031]
[4037]
[4041]
[4151]
%txt1 10x1 1202 cell global
cc =
'p4004'
[4009]
[4005]
[4007]
%cc 4x1 482 cell global
% I need to use ismember or getname function for searching cc rows into txt1 rows like; [row,col] = ismember(txt1,cc) but it works same data type. Is there any solution I can apply or is there any way to transform txt1 and cc matrixes like that;
txt1=[{'p4004'},{'4005'},{'4007'},{'4009'},{'4015'},{'4020'},{'4031'},{'4037'},{'4041'},{'4151'}];
txt1=txt1(:);
cc=[{'p4004'},{'4009'},{'4005'},{'4007'}];
cc=cc(:);
%in here, ismember function(ismember(txt1,cc)) works properly.

Réponse acceptée

Jos (10584)
Jos (10584) le 13 Mai 2014
A = {'text', 1000, 2000, 'data'}
A_as_strings = cellfun(@num2str, A, 'un', 0)
and then you can use ismember ...
  1 commentaire
sermet
sermet le 13 Mai 2014
thanks a lot, it works best.

Connectez-vous pour commenter.

Plus de réponses (2)

Benjamin Avants
Benjamin Avants le 13 Mai 2014
If you're only trying to verify if the entries in cc appear in txt1, you can start by identifying which entries of txt1 are 'char' data type. The inverse of this would be the numerical entries, assuming there are only string and numerical entries.
ex.
strInd = false(size(txt1);
for ii = 1:numel(txt1)
strInd(ii) = ischar(txt{ii});
end
Then do the same for cc and use ismember on the two subsets.
test1 = ismember(cc{strInd2},txt1{strInd});
test2 = ismember(cc{~strInd2},txt1{~strInd});
If you need to know which entries in cc are not in txt1, you can extract them as follows:
temp = cc{strInd2};
textNotFound = temp{~test1};
temp = cc{~strInd2};
numNotFound = temp{~test2};
This may not be the best approach, but it should work. The for loops are not ideal, but I don't know a faster way.
  1 commentaire
sermet
sermet le 13 Mai 2014
it is a little complicated any other solution would be welcomed.

Connectez-vous pour commenter.


Benjamin Avants
Benjamin Avants le 13 Mai 2014
In response to your comments, here is one way to convert cell arrays with different data types to a single type.
You can use a loop with the num2str() function. If the function is passed a string it will return the same string. If it is passed a number, it will return that number as a string.
ex.
num2str('p4505')
ans = 'p4505'
num2str(4505)
and = '4505'
So you can convert your cell array as follows:
for ii = 1:numel(txt1)
txt1{ii} = num2str(txt1{ii});
end
Which will result in txt1 being entirely strings.
You can do the same for the other array and then use ismember to compare them.

Catégories

En savoir plus sur Cell Arrays 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