Problem with ASCII characters in MATLAB
40 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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)
I get what is expected ( http://www.theasciicode.com.ar/extended-ascii-code/degree-symbol-ascii-code-248.html )
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
0 Commenti
Risposte (3)
Walter Roberson
il 4 Ott 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 Commenti
Jurgen vL
il 9 Lug 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
il 9 Lug 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.
Matt Fig
il 4 Ott 2012
Modificato: Matt Fig
il 4 Ott 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 Commenti
Walter Roberson
il 12 Ago 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.)
Vedere anche
Categorie
Scopri di più su Whos in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!