How to compare a structure member with string ?
조회 수: 13 (최근 30일)
이전 댓글 표시
Hi all
I am reading an excel file that contains one string column and one numerical. I want to
compare the strings read from excel with string variables in my code. here in the code
mulnames has a structure like
{'test1'} {[750]}
{'test2'} {[75]}
so I need to compare test1 and test2 names with a variable called filename. but doing the following, I get empty array. ( the condition says, if the first column name is equal
to the variable, take the corresponding number and put it in an array)
mulnames=readcell('Ms.xlsx')
multip=[];
for fe=1:size(fnames)
if strcmp(filename,mulnames(fe,1))
multip=mulnames{fe,2}
end
end
댓글 수: 0
채택된 답변
Walter Roberson
2020년 5월 6일
편집: Walter Roberson
2020년 5월 6일
Do not use size(fnames) there: use numel(fnames)
However you have the problem that you are using number of items belonging to fnames but the array mulnames might have a completely different size unrelated to that.
My guess:
mulnames=readcell('Ms.xlsx');
multip=[];
for fe=1:size(mulnames)
if strcmp(filename,mulnames(fe,1))
multip=mulnames{fe,2};
break
end
end
which can more easily be done as:
mulnames = readcell('Ms.xlsx');
mask = strcmp(filename, mulnames(:,1));
multip = mulnames(mask, 2);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!