How to convert numbers into characters other than in the range 32-127?

1 visualización (últimos 30 días)
Abdul Gaffar
Abdul Gaffar el 7 de Abr. de 2020
Comentada: dpb el 7 de Abr. de 2020
I have the following code:
A = [10, 23, 90, 125, 145, 250];
B = char(A) % Converts double into character
C = ' Z}ú'; % 1x6 char (value of B) copied from 'Workspace'
D = double(C) % converts char into double
%% Conclusion: A and D are not same (why?)
kjkMoreoverM

Respuestas (1)

dpb
dpb el 7 de Abr. de 2020
Editada: dpb el 7 de Abr. de 2020
Because
>> A = [10, 23, 90, 125, 145, 250];
B = char(A)
B =
'
Z}ú'
>> double('
Z}ú')
double('
Error: Character vector is not terminated properly.
>>
isn't at all the same thing as
>> C = ' Z}ú';
>> D = double(C)
You had to convert the linefeed ASCII 10 that was displayed as newline action on the command window into a blank to store it into variable C and so when you convert that string to double you reflect that modification to the original data.
OTOH,
>> B = char(A);
>> all(double(B)==A)
ans =
logical
1
>>
which conclusively illustrates that what is displayed on the terminal screen isn't what is stored in memory. Certain non-printing characters have definite meanings in use to perform actions on the hardware; a char(10) is one of those.
  2 comentarios
Stephen23
Stephen23 el 7 de Abr. de 2020
+1 good point well made.
We get so used to handling vectors of characters that it is easy to overlook the fact that the control characters actually are intended to control particular behaviors of the printer/display and are certainly not intended to be printed verbatim (if that even makes any sense... or is it the sound of one hand clapping?).

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by