Read Structure From C header file

Version 1.0.2 (3.49 KB) by Lior Alpert
This function can create a Matlab structure that match a structure from C header file (.h file)
396 Downloads
Updated 2 Sep 2020

View License

This function reads a structure from a C header file and convert it to Matlab structure.

[MatlabStruct] = Read_C_Struct_From_header(h_FileName);
[MatlabStruct] = Read_C_Struct_From_header(h_FileName, OutStructName);

h_FileName - The name of the h file (e.g. LogStruct.h)
OutStructName - The name of the structure that will be extructed from the header file. If the name doesn't exist in the file or wasn't input by the user, the last structure in the file will be output

The function also supports nested structures.
The struct can be defined by either 'typedef' or 'struct', but doesn't support mix definitions.
for example:

typedef struct {
unsigned long long Var1;
char Var2;
float Var3[8];
} my_struct;

Or:

struct my_struct {
unsigned long long Var1;
char Var2;
float Var3[8];
};

The function is currently limit to the following data types:
[double, single, int8, uint8, int16, uint16, int32, uint32, int64, uint64]
['char', 'short', 'long', 'int', 'long long', 'float', 'double', 'unsigned char', 'unsigned short', 'unsigned long', 'unsigned int', 'unsigned long long']

--------------------------

This function is very useful when trying o read a binary file that was created by C\C++ code. It can be used to to create a structue for the 'cstruct' function:
AJ Johnson (2020). cstruct (https://www.mathworks.com/matlabcentral/fileexchange/4048-cstruct), MATLAB Central File Exchange. Retrieved April 26, 2020.

Cite As

Lior Alpert (2024). Read Structure From C header file (https://www.mathworks.com/matlabcentral/fileexchange/75210-read-structure-from-c-header-file), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020a
Compatible with R2016b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.2

Small fix that prevent potential error with some comments in the C header

1.0.1

1. I have added support in 'bool' type
2. Add an example

1.0.0