how to compare two cells and fetch the values ?

i have two cells,x= A,B,C,D,E,F..and y= B,E,F...if i compare this two cells,i need to the values below B,E,F to be stored to another variable...is it possible?i couldn't figure it out..

댓글 수: 1

dpb
dpb 2013년 10월 28일
What does " the values below B,E,F" mean, precisely?

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

답변 (2개)

sixwwwwww
sixwwwwww 2013년 10월 30일

0 개 추천

Dear Sandy, maybe you can try something like this:
x= ['A','B','C','D','E','F'];
y= ['B','E','F'];
z = reshape(intersect(x, y), [], 1);
I hope it helps. Good luck!

댓글 수: 7

sandy
sandy 2013년 10월 30일
thanks..but what i need is if i give the names of required channel names to a variable like above(y=B,E,F) in a sample.txt file(1*3),it should compare with the headers (x=A,B,C,D,E,F)which is in MSexcel file(6*5000) and copy the matched channels values to another new variable(5000*3)...
Try this:
x= {'A','B','C','D','E','F'};
y= {'B','E','F'};
z = reshape(intersect(x, y), [], 1);
for i = 1:numel(z)
a(:, i) = xlsread('filename.xls', strcat(z{i}, ':', z{i}));
end
disp(a)
Its working now?
sandy
sandy 2013년 10월 30일
편집: sandy 2013년 10월 30일
showing error.........
??? Error using ==> cell.unique at 47 Input must be a cell array of strings.
Error in ==> cell.intersect at 67 a = unique(a);
Error in ==> sss at 10 z = reshape(intersect(raw, fid), [], 1);
sixwwwwww
sixwwwwww 2013년 10월 30일
Can you share your code as well as your files so that I can sort out the error because it seems that the vectors "raw" and "fid" are not in correct format. Looking at code and files will be helpful for finding reason of this error
sandy
sandy 2013년 10월 31일
편집: sandy 2013년 10월 31일
...i need code for this operation(example) in this image...code is
[num, txt, B] = xlsread('input.xlsx');
A = importdata ( 'sample.txt');
for i = 1: numel(A(1,:))
for j = 1: numel(B(1,:))
if ismember(B(1,j),A(1,i))
R(:,i) = B(:,j);
break;
end
end
end
I assume that in your text file you have data as follows:
B E F
and in your excel file you have data as follows:
A B C D E F
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
Now you can read column headers from text file and read data for those column names from excel file as follow:
ID = fopen(TextFileName);
HeaderInfo = textscan(ID, '%s');
fclose(ID);
HeaderInfo = HeaderInfo{:};
[~, ~, raw] = xlsread(ExcelFileName);
ColumnHeaders = raw(1, :);
B = find(ismember(ColumnHeaders, HeaderInfo));
R = cell2mat(raw(2:size(raw,1), B));
I hope it is what you are looking for. Good luck!

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

Jos (10584)
Jos (10584) 2013년 10월 30일

0 개 추천

Use cell array of strings, so you can use all the available set functions:
y = {'A','B','F'}
x = {'A','BBB','C','D','E','F'}
intersect(x,y)
setdiff(x,y)
ismember(y,x)
union(x,y)
setxor(x,y)

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

질문:

2013년 10월 28일

댓글:

2013년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by