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

1 vue (au cours des 30 derniers jours)
Abdul Gaffar
Abdul Gaffar le 7 Avr 2020
Commenté : dpb le 7 Avr 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

Réponses (1)

dpb
dpb le 7 Avr 2020
Modifié(e) : dpb le 7 Avr 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 commentaires
Stephen23
Stephen23 le 7 Avr 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?).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by