Comparing strings with and without ' '

I have two strings but one is 'Word' stored with the quotes '' in a cell array, whereas another is a string that is just : Word without the ''. When I do a string compare, it fails. Is there a solution to this?

답변 (2개)

Sven
Sven 2013년 3월 23일
편집: Sven 2013년 3월 23일

0 개 추천

Hi Mini, first I'll make some strings
myStrings = {'''Word''', 'Word'};
>> myStrings{1}
ans =
'Word'
>> myStrings{2}
ans =
Word
Now, I'll do a string compare:
>> strcmp('Word', myStrings)
ans =
0 1
>> strcmp('''Word''', myStrings)
ans =
1 0
>> strcmp('Word', strrep(myStrings,'''',''))
ans =
1 1
Note that they all "work" (ie, they don't error). Also note how I replaced the quote character with the empty string to make the strcmp return true for both versions.
Does this answer your question?
If not, please show us exactly the code to create your two strings and the comparison you are trying to run. Often this is clearer than a sentence describing code.

댓글 수: 3

Mini
Mini 2013년 3월 24일
Hi, unfortunately this did not work for me. I think the problem may be with the data structure.
The first string is stored by reading an excel file, using this code:
[~,~,raw] = xlsread('data.xlsx', 1, 'A2:B25')
The array 'raw' has cells of string data. I want to compare a 1x1 cell that says 'Word' with a 1x11 char string Word. This keeps failing with strcmp although the string is the same. How do I solve this?
per isakson
per isakson 2013년 3월 24일
Most likely the string isn't the same. Show something the way I did below.
'Word' with a 1x11 char string Word
There are only 4 characters in "Word", or 6 in "'Word'".
Maybe use strtrim() on your variables to get rid of spaces?
You'll note that we can only say things like "maybe" because you keep describing variables rather than just giving code that produces the variable.

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

per isakson
per isakson 2013년 3월 23일
편집: per isakson 2013년 3월 23일

0 개 추천

I'm guessing
>> cac={'Word'}
cac =
'Word'
>> str='Word'
str =
Word
>> strcmp( str, cac )
ans =
1
>>
or less good
>> all( cac{:}==str )
ans =
1

카테고리

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

질문:

2013년 3월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by