필터 지우기
필터 지우기

inconsistency when comparing cell arrays with strings vs char array

조회 수: 3 (최근 30일)
Ian
Ian 2019년 3월 8일
댓글: Ian 2019년 3월 9일
Why does matlab not compare strings & char arrays the same way in aggregate as it does individually?
>> type test_input.m
function test_input(varargin)
disp(strcmp(varargin,"abcd")); % try comparing against string "abcd"
disp(strcmp(varargin,'abcd')); % try comparing against char array 'abcd'...gets same result.
for i=1:length(varargin)
disp(strcmp(varargin{i},"abcd")) % try comparing individually, different result
end
end
>> test_input('abcd','efghi',"abcd")
1 0 0 % <-- match "abcd" with char array 'abcd', but mismatch on string "abcd"
1 0 0 % <-- also match 'abcd' with char array 'abcd', but mismatch on string "abcd"
1 % <-- match 'abcd' with string "abcd:
0
1 % <-- match "abcd" with "abcd". why is this different from aggregate compare????
>> test_input("abcd",'efghi','abcd')
0 0 1 % <-- (same results when switching input around...)
0 0 1
1
0
1
i.e., strcmp(...) doesn't match the input string "abcd" with either 'abcd' or "abcd" when comparing all elements of varargin at once, but does match the input char array 'abcd' with both "abcd" and 'abcd' when comparing in aggregate. However, it does match strings and char arrays properly when comparing individually.
This makes it an interesting challenge for testing for specific strings when the user could call my function with either string text or char array text?...

답변 (2개)

Kevin Phung
Kevin Phung 2019년 3월 8일
'abcd' is considered a 1v4 char.
"abcd" is considered a 1x1 string
  댓글 수: 1
Ian
Ian 2019년 3월 8일
편집: Ian 2019년 3월 8일
Yes, but it actually considers 'abcd' and "abcd" as the same when comparing them individually, but does not consider the identical strings "abcd" and "abcd" as the same when comparing against the entire cell array.

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


James Tursa
James Tursa 2019년 3월 8일
편집: James Tursa 2019년 3월 8일
This is a really good question. E.g.,
>> version
ans =
'9.4.0.813654 (R2018a)'
>> strcmp("abcd","abcd")
ans =
logical
1
>> strcmp({"abcd"},"abcd")
ans =
logical
0
>> strcmp('abcd','abcd')
ans =
logical
1
>> strcmp({'abcd'},'abcd')
ans =
logical
1
>> strcmp({'abcd',"abcd"},'abcd')
ans =
1×2 logical array
1 0
>> strcmp({'abcd',"abcd"},"abcd")
ans =
1×2 logical array
1 0
Not sure why the strcmp treatment of strings should be different when they are part of cell arrays. Maybe this needs to be posted to the TMW bug report system and see what they have to say about it.
As an aside, the ismember( ) function has limitations in this respect also. E.g.,
>> ismember({"abcd"},"abcd")
Error using ismember (line 76)
First argument must be a string array, character vector, or cell array of character vectors.
>> ismember({'abcd'},"abcd")
ans =
logical
1
  댓글 수: 1
Ian
Ian 2019년 3월 9일
Glad I'm not the only one puzzled by this. Am posting as TMW bug report, will update this when they respond.

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

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by