convert complex number string to corresponding character string
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
Prannay Jain
2016-11-14
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.
采纳的回答
Walter Roberson
2016-11-16
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 个评论
Walter Roberson
2016-11-16
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))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!