Results 1 to 5 of 5

Thread: Reading binary file with maltlab format

  1. #1
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Reading binary file with maltlab format

    Hey guys,

    i want to read a binary file which has been read with matlab with this format; 8x double in one line, varying number of lines depending on the file.

    in matlab i can read it like this:

    fid=fopen("filepath),'r')
    initial_state = fread(fid, [8 inf],'double'); #


    from the matlab documentation i found that a matlab double = 64Bits or 8Bytes

    i am using a 64-bit version of qt. This is how i tried to read the file:

    Qt Code:
    1. QFile binary("filepath.bin");
    2. binary.open(QIODevice::ReadOnly);
    3. QDataStream in(&binary);
    4. in.setFloatingPointPrecision(QDataStream::DoublePrecision); //64-bit
    5. double d1,d2,d3,d4,d5,d6,d7,d8;
    6. in >> d1 >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >>d8;
    7. qDebug() << d1 << d2 << d3 << d4 << d5 <<d6 << d7 <<d8;
    To copy to clipboard, switch view to plain text mode 

    From qt documentation:

    QDataStream & QDataStream:perator>>(double & f)

    i think this is what i need but i cant figure out how to use it. The output i am getting are not the output that matlab is reading. can someone point me in the right direction? cheers!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading binary file with maltlab format

    Matlab is unlikely using QDataStream so you don't use it either.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Lumbricus (4th December 2015)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Reading binary file with maltlab format

    If the stored byte order is the same as you machine architecture.
    Qt Code:
    1. Q_ASSERT(sizeof(double)==8);
    2. QFile binary("filepath.bin");
    3. if( binary.open(QIODevice::ReadOnly) ) {
    4. double values[8];
    5. qint64 bytes = binary.read(static_cast<char *>(values), 64);
    6. if (bytes == 64) {
    7. for (int i = 0; i < 8; ++i)
    8. qDebug() << values[i];
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    If the byte order is different then you have a byte swapping exercise to do.

    With a primitive type (i.e. Known size) you might be able to use QDatastream but you still need to know the byte order and set the stream accordingly. That is not really QDataStream's purpose though.

  5. #4
    Join Date
    Oct 2015
    Posts
    33
    Thanks
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Reading binary file with maltlab format

    Thx guys, i took anda's tip and used standard c++ fopen and fread and reverse engineered the code that wrote the binary file to find out what format it has.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading binary file with maltlab format

    You could have still used QFile and its read methods, just using QDataStream was too much

    Cheers,
    _

Similar Threads

  1. qt binary file writing and reading
    By seniorc in forum Newbie
    Replies: 9
    Last Post: 17th December 2013, 23:03
  2. Reading a binary file with QT
    By YuriyRusinov in forum Qt Programming
    Replies: 7
    Last Post: 21st December 2012, 07:14
  3. Binary file reading using Structure
    By umulingu in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2009, 11:35
  4. Reading characters in Binary file.....
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 04:51
  5. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.