catch special characters with strcmp

13 次查看(过去 30 天)
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