Azzera filtri
Azzera filtri

String comparison in cell arrays

66 visualizzazioni (ultimi 30 giorni)
Haritha
Haritha il 24 Apr 2019
Commentato: Haritha il 24 Apr 2019
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'}

Risposta accettata

Jan
Jan il 24 Apr 2019
Modificato: Jan il 24 Apr 2019
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 {{}};

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by