Problem with ASCII characters in MATLAB
24 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
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
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
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
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 个评论
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.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!