String comparison in cell arrays

조회 수: 72 (최근 30일)
Haritha
Haritha 2019년 4월 24일
댓글: Haritha 2019년 4월 24일
Hi, I need to match two cell arrays and need to get matched rows only remaining values i want to make it as empty cells. I am attaching the sample code here. Please let me know if any one knows
filt_data={'PID';'data';'new';'world'};
dat={'1';'2';'3';'4'};
Table1=[filt_data,dat]
ma={'PID';'new';'world'}
for i = 1:size(Table1)
a = Table1(i,1)
for j = 1:size(ma)
b = ma(j,1)
c = strcmp(a,b)
if c==1
Table1{i,1}=ma{j,1};
else
Table1{i,1}={};
end
end
end
I want the output as
Table1 ={'PID','1';'{}','{}';'new','3';'world','4'}

채택된 답변

Jan
Jan 2019년 4월 24일
편집: Jan 2019년 4월 24일
filt_data = {'PID';'data';'new';'world'};
dat = {'1';'2';'3';'4'};
ma = {'PID';'new';'world'};
match = ismember(filt_data, ma);
Table1 = cell(numel(file_data), 2);
Table1(:) = {'{}'}; % Do you really what the char vector '{}'?!
Table(match, 1) = filt_data(match);
Table(match, 2) = dat(match);
Your text mentions "make it as empty cells", but in your example you use the char vector '{}'. I cannot guess, what you really want.
Alternatively:
Table1 = [filt_data(:), dat(:)];
match = ismember(filt_data, ma);
Table1(~match, :) = {'{}'}; % Or: {[]}, or {{}};

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by