reading single-precision floating-point number from binary .dat file written by vb6
23 views (last 30 days)
Show older comments
I have a old program written in vb6 that writes test data into a .dat file using the binary file writer in Visual Basic. I'm trying to read the binary file back into MATLAB, but it seems like VB6 writes numbers in "single" format differently than MATLAB reads it.
here is the code as follows:
fid = fopen('C:\Users\mvandyke\Downloads\csdm000111.dat');
fom = double(fread(fid, 1, 'single'));
mName = char(fread(fid, 60)');
mDateTime = char(fread(fid, 18)');
rating = fread(fid, 1, 'int16');
rpx = fread(fid, 2, 'single');
tracked = fread(fid, 2, 'int8');
numcomps = fread(fid, 1, 'int16');
maxlvl = fread(fid, 1, 'single');
maxexc = fread(fid, 1, 'single');
maxloc = fread(fid, 2, 'int8');
maxord = fread(fid, 1, 'single');
maxlbl = char(fread(fid, 2)');
avgSamples = fread(fid, 1, 'int16');
avgRpm = fread(fid, 1, 'single');
nontriax = fread(fid, 1);
comps = 1:10;
PU = int16(1:10);
ORIENTS = int8(1:30);
CMPNUMS = int16(1:10);
numcols = int16(1:8);
for i = 1:numcomps
comps(i) = fread(fid, 1, 'single');
PU(i) = fread(fid, 1, 'int16');
ORIENTS(3 * (i - 1) + 1) = fread(fid, 1, 'int8');
ORIENTS(3 * (i - 1) + 2) = fread(fid, 1, 'int8');
ORIENTS(3 * (i - 1) + 3) = fread(fid, 1, 'int8');
CMPNUMS(i) = fread(fid, 1, 'int16');
numcols(i) = fread(fid, 1, 'int16');
end
fread(fid, 1);
TBLVALS = 8:100:84;
for k = 1:84
for j = 1:100
for i = 1:8
TBLVALS(i,j,k) = fread(fid,1,'single');
end
end
end
comp1 = permute(TBLVALS(1,:,:), [2 3 1])';
comp2 = permute(TBLVALS(2,:,:), [2 3 1])';
comp3 = permute(TBLVALS(3,:,:), [2 3 1])';
clear i j k fid ans TBLVALS;
it seems to write everything else correctly, such as the char arrays, but the single-precision numbers come out as very high numbers in the positive or negative range. Any advice?
0 Comments
Answers (2)
Walter Roberson
on 21 Nov 2017
Experiment with using swapbytes() on the singles. It might be an endian issue.
When reading binary files, it is recommended that you pass the endian information to fopen() if it is known ahead of time; if you need it to be different for some reads then you can pass the endian information on an fread() call.
0 Comments
Tony Scarcia
on 10 Jan 2018
I use the following to read binary files into matlab. The function below reads the first 10 bytes of the file called 'filename.bin' into an array called sp.
function[sp] = read_cf_file('filename.bin')
format short g;
fileID = fopen('filename.bin', 'r');
sp = fread(fileID,[10],'single');
fclose(fileID);
end
0 Comments
See Also
Categories
Find more on Text Files 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!