catch special characters with strcmp

조회 수: 10 (최근 30일)
Dario Mariniello
Dario Mariniello 2015년 2월 21일
댓글: Geoff Hayes 2015년 2월 21일
Hi, i'm a student and i've just started to use matlab,so i'm inexperienced. I'm developing a function that takes a sting for input with spaces between every single character and using strread and some strcmp i want to recognize every single character. The problem is with special characters:
strcmp('+',buff);
it works fine with all math operators +-*/ (buff contains character i want to ceck and recognize) , but it doesn't with brachets ,=,<, so with other special characters. I found that in strings managing i have to use '' in order to express ' for example, or double percent %% in order to express %, but i didn't find anything about other characters.
Sorry for my inexperience and my bad english!

답변 (2개)

per isakson
per isakson 2015년 2월 21일
편집: per isakson 2015년 2월 21일
buff = '<';
strcmp( '<', buff )
buff = '=';
strcmp( '=', buff )
outputs
ans =
1
ans =
1
  • With the format specifier (e.g with fprintf) "%" is a special character. With strcmp there are no special characters.
  • With regular expressions there is a whole bunch of special characters
  댓글 수: 2
per isakson
per isakson 2015년 2월 21일
편집: per isakson 2015년 2월 21일
Moved to here from separate answer
Do you think it's related with matlab version? I'm using R2011b.
per isakson
per isakson 2015년 2월 21일
No.
Whether or not strread is recommended differs with release.

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


Geoff Hayes
Geoff Hayes 2015년 2월 21일
Dario - I'm not sure if strcmp is the correct function to be using when checking to see if a character of one string belongs in a perhaps larger string. Without showing an example, it isn't clear to my why something like
buff = '<';
strcmp('<',buff)
does not return a one...unless buff is a string with more characters (you allude to this above but then why should *strcmp('+',buff) work correctly).
You may want to consider using either strfind or regexp to match the character that you are looking for within the string. If you wish to use the former, then something like
buff = '1 2 a v < 4 * + ';
strfind(buff,'<')
would return the index of < at 9. If you wish to use the latter, then the appropriate call would be
buff = '1 2 a v < 4 * + ';
regexp(buff,'[<]')
to get the same answer.
  댓글 수: 2
per isakson
per isakson 2015년 2월 21일
>> buff = '1 2 a v < 4 * + ';
>> strsplit( buff )
ans =
'1' '2' 'a' 'v' '<' '4' '*' '+' ''
>> textscan( buff, '%s' )
ans =
{8x1 cell}
>> textscan( buff, '%s' )
ans =
{8x1 cell}
>> ans{:}
ans =
'1'
'2'
'a'
'v'
'<'
'4'
'*'
'+'
>>
Geoff Hayes
Geoff Hayes 2015년 2월 21일
Dario's answer moved here
Sorry goeff, i didn't explain correctly. I use
exp=strread(string, '%s')
(user string input must have spaces between every single character of the equation in order to make format '%s' work properly) that returns to exp an array containing all the characters, than i access every single character using
for i=1:size(exp,1)
buff=exp(i,1);
than i do all my strcmp on buff.I use this particular method because i have to evaluate some characters in base ws using evalin, so i have to 'parse' them one by one.
The problem was that i putted some spaces in the strcmp:
buff= '(';
strcmp(' ( ',buff);
doesn't work of course, becouse of spaces. Really thank you for help, sorry for my inexperience. :)

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

카테고리

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