Problem with ASCII characters in MATLAB

39 visualizaciones (últimos 30 días)
Vahid
Vahid el 4 de Oct. de 2012
Comentada: Walter Roberson el 12 de Ag. de 2022
Hi MATLAB folks,
I have recently encountered a strange problem in the version of MATLAB I use, R2010b (service pack 1).
I noticed that some ASCII characters are missing in it, I mean e.g. when I enter the command:
char(33)
ans =
!
but when I type:
char(248)
the answer MATLAB gives me:
ans =
completely differs from what it should be (°, which is degree symbol).
I would be grateful if anybody could help me to resolve the problem.
Many thanks in advance, --V

Respuestas (3)

Walter Roberson
Walter Roberson el 4 de Oct. de 2012
MATLAB does not use Extended ASCII internally. Instead it uses 16 bit Unicode. And in Unicode, the degree symbol is char(176)
The "Extended ASCII" shown in the chart you linked to is not an international standard. It is instead one particular IBM codepage (#437). If you specifically need the outputs to be in that codepage for some reason, then you need to translate the unicode to the codepage:
unicode2native('°','IBM437')
You will get the 248 output you are expecting. and can then fwrite() that particular sequence of byte value to a file.
  2 comentarios
Jurgen vL
Jurgen vL el 9 de Jul. de 2020
This answer seems to contradict the answer from Matt Fig. You seem to imply MATLAB uses UCS-2, whereas the other answer says it uses a locale specific codepage.
Walter Roberson
Walter Roberson el 9 de Jul. de 2020
MATLAB uses Unicode as its internal character set so that it can represent all letters and symbols, regardless of platform, language, or locale. MATLAB uses UTF-8 as its default character encoding to ensure that all Unicode code points can be correctly represented in files and byte streams. MATLAB also supports other character encodings for backwards compatibility and interoperability.
My reply in 2012 was thus correct, that MATLAB uses 16 bit Unicode internally.
The locale settings can control what the internal bytes get translated to for the purpose of outputting in the active font.

Iniciar sesión para comentar.


Matt Fig
Matt Fig el 4 de Oct. de 2012
Editada: Matt Fig el 4 de Oct. de 2012
From the doc:
"char(X) converts array X of nonnegative integer codes into a character array. Codes from 0 to 127 correspond to ASCII characters, which are uniform across systems. The characters that correspond to higher codes depend upon your current locale setting (see How the MATLAB Process Uses Locale Settings), and codes greater than 65535 are not defined. To convert characters into a numeric array, use the double function."
  3 comentarios
Hammad RIaz
Hammad RIaz el 11 de Ag. de 2022
double('°')
gives
>> double('°')
ans =
176
>> char(176)
ans =
'°'
Walter Roberson
Walter Roberson el 12 de Ag. de 2022
Back in 2012, MATLAB never stored files as UTF8. Unknown characters such as the degree symbol were converted to the international standard character that indicates a substituted character, the SUBS character, which is character position 26.
Modern MATLAB use UTF8 and can represent the degree symbol. (Though at the moment I am not sure if Windows defaults to utf8 support.)

Iniciar sesión para comentar.


Jan
Jan el 4 de Oct. de 2012
Editada: Jan el 4 de Oct. de 2012
This effect depends on the font and character encoding.
Which OS are you using?

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by