Problem with ASCII characters in MATLAB

조회 수: 37 (최근 30일)
Vahid
Vahid 2012년 10월 4일
댓글: Walter Roberson 2022년 8월 12일
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

답변 (3개)

Walter Roberson
Walter Roberson 2012년 10월 4일
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
Jurgen vL
Jurgen vL 2020년 7월 9일
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 2020년 7월 9일
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
Matt Fig 2012년 10월 4일
편집: Matt Fig 2012년 10월 4일
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
Hammad RIaz
Hammad RIaz 2022년 8월 11일
double('°')
gives
>> double('°')
ans =
176
>> char(176)
ans =
'°'
Walter Roberson
Walter Roberson 2022년 8월 12일
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.)

댓글을 달려면 로그인하십시오.


Jan
Jan 2012년 10월 4일
편집: Jan 2012년 10월 4일
This effect depends on the font and character encoding.
Which OS are you using?

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by