cellarray which contains different types of data

조회 수: 69 (최근 30일)
sermet
sermet 2014년 5월 13일
댓글: sermet 2014년 5월 13일
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.

채택된 답변

Jos (10584)
Jos (10584) 2014년 5월 13일
A = {'text', 1000, 2000, 'data'}
A_as_strings = cellfun(@num2str, A, 'un', 0)
and then you can use ismember ...

추가 답변 (2개)

Benjamin Avants
Benjamin Avants 2014년 5월 13일
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
sermet
sermet 2014년 5월 13일
it is a little complicated any other solution would be welcomed.

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


Benjamin Avants
Benjamin Avants 2014년 5월 13일
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.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by