PDA

View Full Version : Reading/Saving binary data



Rasm
02-25-2012, 10:26 PM
Below is my code (somehow the code tags are gone - so sorry-cannot post using code tags). I open the source file(#2) as a binary file and the destination file (#1) for random access. Next I do some large loops reading 4 byte single values from the source file and then save them in the destination file - for every oberservation (iix) - I have 1100 values each 4 bytes to read. As you can see the 1100 values that I read for each of the iix loop is consequtive - so is there a way I read the 1100 byte single values in a single operation and also store them in a single operation. The current loop is quite slow.



For iix = 0 To 20000
AAXdouble = ThisWorkbook.Worksheets("TempSample").Cells(iix, ColSpectraPosition)
For iiix = 0 To 9999
Get #2, Adouble, Asingle
Put #1, AXdouble, Asingle
Adouble = Adouble + 4
AXdouble = AXdouble + 4
Next iiix
Next iix

Rasm
02-26-2012, 07:14 AM
ok - I figured out how to read a chunck of data - It is as simple as using a string buffered with in this case 4400 spaces



Absorbancies = Space$(4400) ' Set our buffer (string) to that length.


Get #2, Adouble, Absorbancies ' Grab a chunk of data from the file.
Put #1, AXdouble, Absorbancies

Excel Fox
02-26-2012, 03:00 PM
Thanks for sharing the solution Rasm.... can you post the whole code for posterity

Rasm
02-26-2012, 08:15 PM
Read/Write chunks of binary data - I have simplified the code here - so this is simply an example - you have to set your own paramters

The code listed here – reads chunks of data from one binary file and saves it to another binary file. In this example I have 20K observations (samples) – for each observation I have 1100 single precision values. This code reads all the 1100 single values for each observation in one operation rather than a loop reading 1100 single values one by one. That way the code is much faster.



Public Sub ReadWriteBinary(ByVal PathNameSourceFile As String, ByVal PathNameDestinationFile As String)

Dim Buffer As String
Dim NumberOfValues As Long, Xlong As Long

' The number of blocks of data - In this case I have 20K records where I want to copy data
NumObservations = 20000
' This example has 1100 values each a 4 byte number of the data type single precision.
NumberOfValues = 1100
'In this example I want to read a series of 4 byte Single data types (Single) for each observation
Xlong = 4 * NumberOfValues
' Set the buffer for each chunk of data to read
Buffer = Space$(Xlong)

Open PathNameSourceFile For Binary As #2
Open PathNameDestinationFile For Binary As #1
For iix = 0 To NumObservations - 1
' This is the location in Bytes where the source data reside
LocalOfDataSource = iix * 5000 + 512
'This is the location in Bytes where I want to save the data
LocalOfDestinationData = iix * 8000 + 1024
' reads a chunk of data from the source file.
Get #2, LocalOfDataSource, Buffer
'Saves the data just read to the destination file
Put #1, LocalOfDestinationData, Buffer
Next iix
Close #1
Close #2
End Sub