Equalities between strings and strings in cell arrays

조회 수: 2 (최근 30일)
Nikolay Rodionov
Nikolay Rodionov 2012년 11월 21일
I am trying to create a piece of code that checks the string value in a cell array and then assigns a numerical value to another cell based on the identity of that string.
However, no matter what equality function I try to use, I always get an error back or an inequality when both values are obviously the same. Can someone explain what is going on to me?
Here's the hunk of code that I am having trouble with. If the cell array 'C' and string 'C' are the same then I should get an output of 1.
>> tbl{row,2}(1)
ans =
'C'
>> isequal(tbl{row,2}(1),'C') == 1
ans =
0

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 11월 21일
편집: Andrei Bobrov 2012년 11월 21일
try
isequal(tbl{row,2}{1},'C')
or
isequal(tbl{row,2}(1),{'C'})

Jan
Jan 2012년 11월 21일
편집: Jan 2012년 11월 21일
This checks the fist character:
strncmp(tbl(row, 2), 'C', 1)
This compares the full string:
strcmp(tbl(row, 2), 'C')
Note, that row need not be scalar in both cases.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by