String comparison in cell arrays

67 ビュー (過去 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 {{}};
  1 件のコメント
Haritha
Haritha 2019 年 4 月 24 日
Thanks a lot

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by