How do i get Matlab to recognize an input file in two's complement?

3 views (last 30 days)
Hey,
I use MATLAB 7.9.0 (R2009b).
I'm trying to input a file generated from a modelsim simulation, and I want matlab to identify the data as two's complement, and then convert it to decimal. For instence - 0100.010100 would be converted to 4.3125.
The file contents:
0100.010100
0010.001010
0001.000101
0000.100011
0000.010001
0000.001001
0000.000100
0000.000010
0101.001100
0010.100110
0110.011110
1000.011010
1001.011000
1001.110111
1010.000110
1010.001110
1010.010010
0101.001001
0010.100101
0001.010011
The best I could do is get Matlab to identify it as decimal, and then manually write a function which converts the numbers to decimal according to two's complement rules. Got a few bugs there, and it doesn't make sence that I need to go that way...
Is there any easier way to do this.
BTW, I can manipulate the manner in which the data is written in the file (from the simulation). It can be decimal (although the RHS won't be considered two's decimal when writing to file), I can put a space instead of "." Almost any manipulation possible - except writing in true decimal value.
Thanks!
-Josh

Answers (1)

Walter Roberson
Walter Roberson on 19 Feb 2014
Use no delimiter at all, just 10 binary digits in a row. Read the lines as strings. Use bin2dec() on the strings. Divide the result by 2^6. Any result that is 8 or greater, subtract that result from 8 to get the negative number being represented.

Categories

Find more on Numeric Types 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!