convert complex number string to corresponding character string

조회 수: 12 (최근 30일)
Shafali
Shafali 2016년 11월 10일
댓글: Walter Roberson 2016년 11월 16일
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
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.
Shafali
Shafali 2016년 11월 15일
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)?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by