Problem with ASCII characters in MATLAB

38 vues (au cours des 30 derniers jours)
Vahid
Vahid le 4 Oct 2012
Commenté : Walter Roberson le 12 Août 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

Réponses (3)

Walter Roberson
Walter Roberson le 4 Oct 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 commentaires
Jurgen vL
Jurgen vL le 9 Juil 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 le 9 Juil 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.

Connectez-vous pour commenter.


Matt Fig
Matt Fig le 4 Oct 2012
Modifié(e) : Matt Fig le 4 Oct 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 commentaires
Hammad RIaz
Hammad RIaz le 11 Août 2022
double('°')
gives
>> double('°')
ans =
176
>> char(176)
ans =
'°'
Walter Roberson
Walter Roberson le 12 Août 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.)

Connectez-vous pour commenter.


Jan
Jan le 4 Oct 2012
Modifié(e) : Jan le 4 Oct 2012
This effect depends on the font and character encoding.
Which OS are you using?
  1 commentaire
Vahid
Vahid le 4 Oct 2012
my OS is MAC OSX.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by