简体   繁体   中英

Matlab: ASCII to floating point

I have an ASCII reading from an instrument:

read4 = '..V3..V3..V3..V3..V3..V3..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3'

I need to convert, in Matlab, this reading into a floating point numbers. Each 4 bytes from 'read' represents a floating point value. How to do this conversion in Matlab?

In C++ I can use printf:

FILE *inf = fopen("input.txt", "rb");
int i = 0;
float f;
while (fread(&f, sizeof(f), 1, inf) == 1) {
   printf("%s\t", f);
   printf("%d %e\n", i++,f);

}

And I get the following result:

..V3..V3..V3..V3..V3..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    0 4.986776e-08
..V3..V3..V3..V3..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    1 4.986776e-08
..V3..V3..V3..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    2 4.986776e-08
..V3..V3..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    3 4.986776e-08
..V3..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    4 4.986776e-08
..V3..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    5 4.986776e-08
..V3..V3..V3,.V36.V3..V3z.V3..V36.V3    6 4.986776e-08
..V3..V3,.V36.V3..V3z.V3..V36.V3    7 4.986776e-08
..V3,.V36.V3..V3z.V3..V36.V3    8 4.986776e-08
,.V36.V3..V3z.V3..V36.V3    9 4.986776e-08
6.V3..V3z.V3..V36.V3    10 4.986775e-08
..V3z.V3..V36.V3    11 4.986779e-08
 z.V3..V36.V3   12 4.986776e-08
..V36.V3    13 4.986803e-08
6.V3    14 4.986776e-08
    15 4.986779e-08

I have available also a 15 bits precision double number, which should be converted using 8 bytes of ASCII :

read8 = '...G\.j>%bc.q.j>.&....j>...T1.j>c.'...j>...G\.j>c.'...j>c.'...j>......j>pn]{..j>..?...j>..E.F.jpn]{..j>.zW5..j>..!.Z.j>..E.F.j>UD.:..j>..E.F.j>......j>UD.:..j>.&....j>..!.Z.j>UD.:..j>......j>.zW5.j>..E.F.j>pn]{..j>......j>...G\.j>..!.Z.j>...T1.j>...G\.j>.'

Thank you!

try using typecast

>> typecast( int8( read4 ), 'single' )
ans =
1.0e-007 *
0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987    0.4987

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM