Why does strcmp return 0 in in the matlab script while 1 on consol

3 visualizaciones (últimos 30 días)
Hi,
I wonder what mistake I am doing!
When I run the following code on console I get correct output (flag=1) while the same code evaluates to (flag=0) when run in matlab script file. I am trying with Hindi unicode characters. During debug watch I get same values as expected in the variable but evaluation is not as expected. I tried with isequal() function also. but it results same behavior.
-------------
previous='ग';
character='ा';
if strcmp(previous,'ग') && strcmp(character,'ा')
flag=1
else
flag=0
end
-------------------------

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Nov. de 2015
In R2014a on OS-X, I find that there is a difference between these two lines:
previous='ग'; %works
previous='ग' %assigns char(26)
Experimenting, it appears that any literal character with char position beyond 255 gets converted to char(26) if it appears in any context other than an assignment terminated with a semi-colon.
This is, of course, a bug. I have no information about which versions it affects.
One thing you need to be very careful with is that when you Save a .m file and you are using the English version of MATLAB (at least up through R2014a in OS-X), then the file is saved as 8-bit characters not as UTF-8, so any literal character beyond char(255) will become a sequence of characters rather than a single character. For example, char(300) would become the pair [char(1), char(44)] in the file. I gather that the non-English versions of MATLAB (or possibly later versions of MATLAB) offer an option to save the source file as UTF-8; my version does not offer that.
  4 comentarios
Walter Roberson
Walter Roberson el 7 de Nov. de 2015
In OS-X R2014a, at the console, using the English version of MATLAB,
>> char_1=double('रै')
char_1 =
26 26
The problem affects characters in quotes at the console in any context except
variable='string';
The 63 that you see in the file happens when you save the file without telling it to save it as UTF-8 (which is not an option on R2014a OS-X English version. If you close the file and open it again you will see that all of the Hindi characters have been replaced by '?'.
The problem you have is not with comparing Hindi strings that are already in variables: the problem is with getting particular characters into code. And the way to do that in a portable manner is to never use the quoted characters at all, and to instead use their unicode positions. For example, instead of coding
strcmp(previous, 'मतलब')
code
h_TA = char(2340);
h_BA = char(2348);
h_MA = char(2350);
h_LA = char(2354);
h_MATALABA = [h_MA h_TA h_LA h_BA];
if strcmp(previous, hMATALABA)
Rajesh M
Rajesh M el 26 de Nov. de 2015
Editada: Rajesh M el 26 de Nov. de 2015
Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by