Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:53:18+00:00 2026-05-28T05:53:18+00:00

I’m coding a function in C++ to load a MAT-File (level 5) format based

  • 0

I’m coding a function in C++ to load a MAT-File (level 5) format based on the MATLAB®
MAT-File Format 2011b
doc (see http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf).

I must be missing something (maybe with C++) because the number of bytes field is zero. The MAT-File header is read successfully as is the data type element flag but the number of bytes is not. The piece of code to load the mat file is as follows:

// file handler
ifstream file;
// open file
file.open(i_file, ifstream::in | ifstream::binary);
// check for errors
if (!file) return NULL;

/********** BEGIN MAT-File Header **********/
char header_text[116], header_offset[8], header_version[2], header_endian[2];
// The first 116 bytes of the header can contain text data in human-readable form.
file.read( (char*) &header_text, 116); cout << header_text << endl;
/* Header Subsystem Data Offset Field */
// Bytes 117 through 124 of the header contain an offset to subsystem-specific
// data in the MAT-file. 
file.read( (char*) &header_offset, 8); cout << header_offset << endl;
/* Header Flag Fields */
// Version When creating a MAT-file, set this field to 0x0100.
file.read( (char*) &header_version, 2); cout << header_version << endl;
// Endian Indicator. Contains the two characters, M and I, written to the
// MAT-file in this order, as a 16-bit value. 
file.read( (char*) &header_endian, 2); cout << header_endian << endl;

/********** END MAT-File Header **********/

/********** BEGIN MAT-File Data Element **********/

/* The Tag Field */
// The 8-byte data element tag is composed of two, 32-bit fields 
// Data Type
__int32_t data_type = file.get(); cout << data_type << endl;
// Number of Bytes
__int32_t num_bytes = file.get(); cout << num_bytes << endl;

The output is as follows:

MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri May 20 18:21:46 2011


IM
15
0

From MATLAB I get the information:

whos -file PaviaU.mat

Name Size Bytes Class Attributes

paviaU 610x340x103 170897600 double

Did I incorrectly load the data from the header somehow? Why is Number of bytes equal to zero?

Edit: If I read the data elements as follows (sa suggested in one comment):

char data_type[4], num_bytes[4];
file.read((char*) &data_type, 4); cout << data_type << endl;
file.read((char*) &num_bytes, 4); cout << num_bytes << endl;

I get unexpected values on the cout (binary codes)

But debugging the function I can inspect both variables:

data_type[0] = 15
data_type[1] = 0 '\0'
data_type[2] = 0 '\0'
data_type[3] = 0 '\0'

num_bytes[0] = -3/253
num_bytes[1] = 27
num_bytes[2] = 19
num_bytes[3] = 2

The data_type value is 15 but what about the -3/253 in num_bytes? Which number is that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:53:19+00:00Added an answer on May 28, 2026 at 5:53 am

    According to the spec, your results shown the data type is

    miCOMPRESSED: Compressed Data

    The size field shows your data size is 0x02131BFD or 34,806,781 Bytes. Compared to your original size of 170MB, this compression ratio seems reasonable depending on your data.

    Assuming you can spare the 140MB, it would probably be easier to save the file as uncompressed data. Saving as an old .mat version disabled compression (mathworks). I don’t know of a way to disable it in new .mat files.

    Edit

    The size and data type fields can be better read as:

    uint32_t data_type, num_bytes;
    file.read(reinterpret_cast<char*>(&data_type), sizeof(uint32_t));
    file.read(reinterpret_cast<char*>(&num_bytes), sizeof(uint32_t));
    

    This can be done directly as your machine is little endian. Has the result of the endian field been the opposite, you would have to swap the order of all the bytes before storing them in the uint32.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.