COMPARE CELLS VIA A LOOP

1 回表示 (過去 30 日間)
Ivan Mich
Ivan Mich 2021 年 1 月 20 日
コメント済み: Walter Roberson 2021 年 1 月 20 日
Hello,
I have a problem with a code. I have two columns with cell (200x1 cell) in a csv file. I would like to compare these two columns, and escpecially the first letter of them.
I wrote this code:
clc
clear
%comparison
filename1 = 'test.xlsx';
[d1,tex]= xlsread(filename1);
NAME1=tex(:,1);
k1=strcat(NAME1{1}(1)) %first letter
NAME2=tex(:,3);
k2=strcat(NAME2{1}(1)) %first letter
ndata=1:size(NAME1,1)
but I do not know how to continue it.
I think if statement would help. I mean:
for i
if k1==k2
write 'aggree'
else write 'disaggree'
end
end
Could you please help me?

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 1 月 20 日
%comparison
filename1 = 'test.xlsx';
[d1,tex]= xlsread(filename1);
NAME1=tex(:,1);
k1 = cellfun(@(C) C(1), NAME1); %first letter
NAME2=tex(:,3);
k2 = cellfun(@(C) C(1), NAME2); %first letter
mask = k1 == k2;
choices = {'disagree', 'agree'};
results = choices(mask+1);
  2 件のコメント
Ivan Mich
Ivan Mich 2021 年 1 月 20 日
Excuse me again, If I want to create a new .xlsx file with the disagrees which command should I use?
Walter Roberson
Walter Roberson 2021 年 1 月 20 日
d=[NAME1(~mask), NAME2(~mask)] ;
now writecell or xlswrite d

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by