Amin Ya
Amin Ya

Reputation: 1958

MATLAB/Embedded Coder File Loading

I generated code for loading a mat file as follows

data=coder.load('data.mat');
a=data.a;
b=data.b;

Because one of the variables, for example "a", is very big, it is defined as a big static const array in the main function with all values initialized there.

Is there any way I can make MATLAB Coder load data from a file in C Code instead of defining it as a variable in main function?

Upvotes: 1

Views: 543

Answers (2)

Amin Ya
Amin Ya

Reputation: 1958

This is the exact code that we should use based on Ryan's answer:

load('Data.mat')
fileID = fopen('Data.bin', 'w');
fwrite(fileID, Matrix1,'uint64');
fclose(fileID);


fileID=fopen('Data.bin');
Matrix2=fread(fileID,[256,256],'uint64');
fclose(fileID);

Matrix 2 is now the same as Matrix 1. The trick for writing and reading is to use the same precision based on the data type.

Upvotes: 1

Ryan Livingston
Ryan Livingston

Reputation: 1928

The MATLAB fread function is supported for code generation. So you can fwrite the data to a file in MATLAB and then fread it in the generated code. This will do a runtime read and avoid the giant constant in the generated code.

Upvotes: 3

Related Questions