How can I read big-endian binary files with embedded coder?

25 views (last 30 days)
I have a function in MATLAB that I am using to read some legacy binary files that use Big-endian format for bit ordering. It contains a few lines of code similar to:
% Open Binary File
fileID = fopen('myBinaryFile.xyz');
% Read from Binary file to structure
structure.fieldnameA = fread(fileID,[1 2],'*unit16','ieee-be');
structure.fieldnameB = fread(fileID,[1 1],'unit16=>char','ieee-be');
% Close file
fclose(fileID)
Where the sizes and types are picked here arbitarlity. I use the 'machineformat' argument in fread to order the bits correctly. This (appears) to work fine inside the MATLAB environment and I can generate a structure with the data I need.
However, I need to use this functionality inside a simulink model. Attempting to put this function into an embedded MATLAB block returns the error:
> For code generation, you cannot use the 'machineformat' input argument.
I also get a similar error when attempting to use MATLAB coder to generate C/C++ code that could be used in an S-Function.
My question:
Does there exist a code generation friendly version of the machineformat argument? If not, is it possible to write a function that has the same functionality, but is code generation friendly?

Accepted Answer

Swatantra Mahato
Swatantra Mahato on 9 Jul 2021
Hi Ben,
It is my understanding that you wish to be able to change the byte ordering of your input in a code generation setting. The function "fread" does not support "machinefmt" input for code generation as is mentioned in the documentation. You can look into the following funcitons to work around this issue
  • "coder.ceval" - lets you call external C/C++ function from your MATLAB code.
  • "swapbytes" - swaps the byte ordering of the input and can be used with code generation
Hope this helps

More Answers (0)

Community Treasure Hunt

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

Start Hunting!