convert complex number string to corresponding character string

13 vues (au cours des 30 derniers jours)
Shafali
Shafali le 10 Nov 2016
Commenté : Walter Roberson le 16 Nov 2016
hey I just want to convert a large complex number string into its corresponding character string. My code is:
u=w+zne;
h=double(u); %I used double() because 'u' is a symbolic variable.
enc=num2str(h);
If zne=(1e+2+22e+3i) then num2str works but if I increases the value of zne like (123e+3+23e+4i) then it returns result in number string form as [1.234104e+061.234105e+061.234044e+061.234116e+0...] I require result in character form as [ansdfff...].
Please help me...
  2 commentaires
Prannay Jain
Prannay Jain le 14 Nov 2016
What are the values of u, w, h and enc in your case? When I used num2str(123e+3+23e+4i) it worked fine. I need to check other values to reproduce the issue.
Shafali
Shafali le 15 Nov 2016
Sample Code:
function final=demo()
a=133 + 232i;
fi=fopen('abc.txt','r');
data=fscanf(fi,'%s');
fclose(fi);
doubledata=double(data);
res=doubledata+a;
final=num2str(res,'%c');
end
Output: ÉèiêèiòèiôèiËèiîèiñèiêèi±èiíèiîèi±èiùèiíèiîèiøèiîèiøèiøèiíèiæèiëèiæèiñèiîèi³èi
If I do little change in the value of a like
a=133e+4 + 232e+5i;
Then the output is :1.330068e+06+2.320000e+07i1.330101e+06+2.320000e+07i1.330109e+06+2.320000e+07i1.330111e+06+2.320000e+07i1.330070e+06+2.320000e+07i1.330105e+06+2.320000e+07i1.330108e+06+2.320000e+07i1.330101e+06+2.320000e+07i1.330044e+06+2.320000e+07i1.330104e+06+2.320000e+07i1.330105e+06+2.320000e+07i1.330044e+06+2.320000e+07i1.330116e+06+2.320000e+07i1.330104e+06+2.320000e+07i1.330105e+06+2.320000e+07i1.330115e+06+2.320000e+07i1.330105e+06+2.320000e+07i1.330115e+06+2.320000e+07i1.330115e+06+2.320000e+07i1.330104e+06+2.320000e+07i1.330097e+06+2.320000e+07i1.330102e+06+2.320000e+07i1.330097e+06+2.320000e+07i1.330108e+06+2.320000e+07i1.330105e+06+2.320000e+07i1.330046e+06+2.320000e+07i
can I get the output in character form (may be in special symbol as it is in encrypted form)?

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 16 Nov 2016
The result of your fscanf is system dependent, and depends upon your region settings and some environment variables in order to know how the bytes in the file are going to be converted to characters. I recommend that you supply the encoding parameter to the fopen call, such as 'UTF-8' or 'ISO8896-1'
Without that knowledge it is hard for us to talk about what is happening to the bytes.
You add 232e+5 to the imaginary component of what you have on input. That value exceeds 65536 which is the largest representable character position in MATLAB.
  1 commentaire
Walter Roberson
Walter Roberson le 16 Nov 2016
You are using the %c format specifier. That specifier is for taking non-negative integer character numbers and marking them as characters, like position #48 is '0', which is something that does not involve any formatting at all. Your values are outside the range of integers from 0 to 65535, so your values are being treated as if a %e format had been written, which is what fprintf() and related routines do with values out of range for the given format -- use %e format instead.
You can format an imaginary number as decimal values by using one of the numeric formats %e, %f, %g, applied to the real and imaginary portions. For example,
final = sprintf('%g%+gi', real(res), imag(res))

Connectez-vous pour commenter.

Plus de réponses (0)

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