convert complex number string to corresponding character string
14 views (last 30 days)
Show older comments
Shafali
on 10 Nov 2016
Commented: Walter Roberson
on 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 Comments
Prannay Jain
on 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.
Accepted Answer
Walter Roberson
on 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 Comment
Walter Roberson
on 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))
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!