Results 1 to 6 of 6

Thread: BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    3

    Lightbulb BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

    Dear community,

    I am trying to develop a VBA macro to read a binary file.
    I go through the forum and make some try but I didn't achieve to make it work.

    I am so then kindly asking for some help.

    The binary file is including data only (no title) an is provided in attachment (both binary and ascii)
    binary.txt to be read by excel to have ascii.txt

    I easily get the information using matlab and the below code :
    fid = fopen('binary.txt');
    Data_bin = fread(fid,'float32')';
    fclose(fid);

    I try to reproduce this using Excel VBA to read the information directly from the binary.txt.

    Below is my attemp catch from another forum

    Sub Temp()
    Dim intFileNum%, bytTemp As Byte, intCellRow%
    intFileNum = FreeFile
    intCellRow = 0
    Open "binary.txt" For Binary Access Read As intFileNum
    Do While Not EOF(intFileNum)
    intCellRow = intCellRow + 1
    Get intFileNum, , bytTemp
    Cells(intCellRow, 1) = bytTemp
    Loop
    Close intFileNum
    End Sub



    Any help would be very appreciate/
    Last edited by CBO789; Aug 10th, 2021 at 04:08 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

    Quote Originally Posted by CBO789 View Post
    I am trying to develop a VBA macro
    Then what possible reason could you have for posting in the VB.NET forum? I have asked the mods to move this thread to the right place.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    3

    Re: BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

    Hi jmcilhinney.

    You are fully right. Sorry for the mistake.
    I try to move it by myself but didn't managed yet.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

    Quote Originally Posted by CBO789 View Post
    Hi jmcilhinney.

    You are fully right. Sorry for the mistake.
    I try to move it by myself but didn't managed yet.
    Users cannot move or delete their own threads. I have reported the thread to the mods and asked that it be moved, so hopefully they get to it soon. It can depend who's online in which timezone. I'm in Australia so I keep different hours to most of the mods.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

    you can test this to see if it does what you require
    Code:
    Dim intFileNum%, bytTemp As String, intCellRow%
    intFileNum = FreeFile
    Open "c:\temp\ascii.txt" For Binary Access Read As intFileNum   ' change path to suit
    Z = Input(LOF(intFileNum), intFileNum)
    s = Split(Z, vbNewLine)
    For i = 0 To UBound(s)
        l = Split(s(i), vbTab)
        For J = 0 To UBound(l)
            Cells(i + 1, J + 1) = l(J)
        Next
    Next
    if you want all the values in a single column, just a small change to one line
    change the variable names to something meaningful to you

    NOTE you will need to format the excel cells to scientific with the appropriate number of decimal places to display the values correctly, this can also be done by code if required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    3

    Re: BA macro to read a binary file (equivalent to Mtalab Fread with float32 option)

    Hi westconn1,

    Sorry for my late answer, and thank you for your code.
    if I am not mistaken, your code proposal helps to put ascii.txt file content into excel cells.

    Thank you for that but my issue is to manage to read with Excel the file binary.txt in order to het its content in and ascii format in Excel.
    (ascii.txt content is the target I get to have by reading with excel the binary.txt file)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width