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

28 ビュー (過去 30 日間)
Ben McMahon
Ben McMahon 2021 年 7 月 6 日
回答済み: Swatantra Mahato 2021 年 7 月 9 日
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?

採用された回答

Swatantra Mahato
Swatantra Mahato 2021 年 7 月 9 日
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

その他の回答 (0 件)

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by