![]() % Copyright (C)2007 PerkinElmer Life and Analytical Sciences % Stephen Westlake, Seer Green % % History % SW Initial version % misc: miscellanous information in name,value pairs ![]() % % = spload(filename): % data: 1D array of doubles % xAxis: vector for abscissa (e.g. % Note that earlier 'Data Manager' formats are not supported. % This version supports 'Spectrum' SP files. integers in 16 bits and 32 bits respectively, 64 bits double precision variables, string connected by several 8 bit "uchar" characters, which are all well supported by Matlab or C++. There are different variable types supported in Matlab, but in VBA, how to read similar binary file correctly? I tried to use a subrontine like this, but don't know how to skip the file header and go to desired data directly and how to read desired variables in desired format. I want to use VBA to read Perkin Elmer's binary *.sp spectra file, but I have no idea of the format details of the *.sp except that I know that the following Matlab function can be usesd for Perkin Elmer binary *.sp Spectral file data importing. ' If the file was created using FileWriteBinary, this will be the number ' of times data was appended to the file.įunction FileReadBinary(sFileName As String) As Variant Dim iFileNum As Integer, lFileLen As Long Dim vThisBlock As Variant, lThisBlock As Long, vFileData As Variant On Error GoTo ErrFailed If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then iFileNum = FreeFile Open sFileName For Binary Access Read As #iFileNum lFileLen = LOF(iFileNum) Do lThisBlock = lThisBlock + 1 Get #iFileNum,, vThisBlock If IsEmpty(vThisBlock) = False Then If lThisBlock = 1 Then ReDim vFileData(1 To 1) Else ReDim Preserve vFileData(1 To lThisBlock) End If vFileData(lThisBlock) = vThisBlock End If Loop While EOF(iFileNum) = False Close iFileNum FileReadBinary = vFileData End IfĮxit Function ErrFailed: Close iFileNum Debug.Print Err.Description End Function ArrayResults(1 to lNumDataBlocks) ' Where lNumDataBlocks is the number of data blocks stored in file. 'Purpose : Reads the contents of a binary file 'Inputs : sFileName The path and file name where the data is stored 'Outputs : Returns an array containing all the data stored in the file. ' sFileName The path and file name where the data is to be stored ' If True will append the data to the existing file 'Outputs : Returns True if succeeded in saving data 'Notes : Saves data type (text and binary).įunction FileWriteBinary(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = True) As Boolean Dim iFileNum As Integer, lWritePos As Long On Error GoTo ErrFailed If bAppendToFile = False Then If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then 'Delete the existing file VBA.Kill sFileName End If End If iFileNum = FreeFile Open sFileName For Binary Access Write As #iFileNum If bAppendToFile = False Then 'Write to first byte lWritePos = 1 Else 'Write to last byte + 1 lWritePos = LOF(iFileNum) + 1 End If Put #iFileNum, lWritePos, vData Close iFileNum FileWriteBinary = True Exit FunctionĮrrFailed: FileWriteBinary = False Close iFileNum Debug.Print Err.Description End Function Can be an ' array or any simple data type. 'Purpose : Saves/writes a block of data to a file 'Inputs : vData The data to store in the file. ![]() Below the example code can only read and write "Variant" variables. Hello, does anyone know how to write adn read 16 bit and 32 bit integer in a binary file? I am just wondering how VBA can handle different variables.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |