Read a file contains hexdecimal complex numbers, and convert decimal complex numbers
Show older comments
Hi All,
I want to read a file contains hexdecimal complex numbers, and convert decimal complex numbers.
Each line has one hexdecimal complex number. I want to read all lines, and conver the decimal complex number (real and imag parts) and store them in an array. I tried textread, textscan, and so on. I also tried to read them as "str" and split them into two part and convert and combine later. However, all of my approaches failed actually. Please help!
<real_imag.txt>
fffd+0000*i
0016+0000*i
0000+ffff*i
.
.
.
Accepted Answer
More Answers (2)
Walter Roberson
on 24 Dec 2019
Edited: Walter Roberson
on 24 Dec 2019
There are three possible reasonable interpretations:
fid = fopen('real_imag.txt', 'rt');
data_cell = textscan(fid, '%4x%c%4x');
fclose(fid);
csign = (data_cell{2}=='+')*2-1;
%case where components are unsigned 16 bit and the complex sign is guaranteed positive:
vals_uint16 = complex(uint16(data_cell{1}), uint16(data_cell{3}));
%case where components are unsigned 16 bit but the complex sign is not guaranteed positive. negative of an unsigned 16 bit needs more than 16 bits
vals_int32 = complex(int32(data_cell{1}), int32(csign) .* int32(data_cell{3}));
%case where components are signed 16 bit and the complex sign is positive just as a notation placeholder
vals_int16 = complex(typecast(uint16(data_cell{1}),'int16'), typecast(uint16(data_cell{3}),'int16'));
Jaeyoung
on 24 Dec 2019
0 votes
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!