Main Content

coder.load

Load compile-time constants from MAT-file or ASCII file

Description

S = coder.load(filename) loads compile-time constants from filename.

  • If filename is a MAT-file, then coder.load loads variables from the MAT-file into a structure array.

  • If filename is an ASCII file, then coder.load loads data into a double-precision array.

coder.load loads data at code generation time, also referred to as compile time. If you change the content of filename after you generate code, the change is not reflected in the behavior of the generated code.

example

S = coder.load(filename,var1,...,varN) loads only the specified variables from the MAT-file filename.

S = coder.load(filename,'-regexp',expr1,...,exprN) loads only the variables that match the specified regular expressions.

S = coder.load(filename,'-ascii') treats filename as an ASCII file, regardless of the file extension.

S = coder.load(filename,'-mat') treats filename as a MAT-file, regardless of the file extension.

S = coder.load(filename,'-mat',var1,...,varN) treats filename as a MAT-file and loads only the specified variables from the file.

S = coder.load(filename,'-mat','-regexp', expr1,...,exprN) treats filename as a MAT-file and loads only the variables that match the specified regular expressions.

Examples

collapse all

Generate code for a function edgeDetect1 which given a normalized image, returns an image where the edges are detected with respect to the threshold value. edgeDetect1 uses coder.load to load the edge detection kernel from a MAT-file at compile time.

Save the Sobel edge-detection kernel in a MAT-file.

k = [1 2 1; 0 0 0; -1 -2 -1];

save sobel.mat k

Write the function edgeDetect1.

function edgeImage = edgeDetect1(originalImage, threshold) %#codegen
assert(all(size(originalImage) <= [1024 1024]));
assert(isa(originalImage, 'double'));
assert(isa(threshold, 'double'));

S = coder.load('sobel.mat','k');
H = conv2(double(originalImage),S.k, 'same');
V = conv2(double(originalImage),S.k','same');
E = sqrt(H.*H + V.*V);
edgeImage = uint8((E > threshold) * 255);

Create a code generation configuration object for a static library.

cfg = coder.config('lib');

Generate a static library for edgeDetect1.

codegen -report -config cfg edgeDetect1

codegen generates C code in the codegen\lib\edgeDetect1 folder.

Generate code for a function edgeDetect2 which given a normalized image, returns an image where the edges are detected with respect to the threshold value. edgeDetect2 uses coder.load to load the edge detection kernel from an ASCII file at compile time.

Save the Sobel edge-detection kernel in an ASCII file.

k = [1 2 1; 0 0 0; -1 -2 -1];
save sobel.dat k -ascii

Write the function edgeDetect2.

function edgeImage = edgeDetect2(originalImage, threshold) %#codegen
assert(all(size(originalImage) <= [1024 1024]));
assert(isa(originalImage, 'double'));
assert(isa(threshold, 'double'));

k = coder.load('sobel.dat');
H = conv2(double(originalImage),k, 'same');
V = conv2(double(originalImage),k','same');
E = sqrt(H.*H + V.*V);
edgeImage = uint8((E > threshold) * 255);

Create a code generation configuration object for a static library.

cfg = coder.config('lib');

Generate a static library for edgeDetect2.

codegen -report -config cfg edgeDetect2

codegen generates C code in the codegen\lib\edgeDetect2 folder.

Input Arguments

collapse all

Name of file. filename must be a compile-time constant.

filename can include a file extension and a full or partial path. If filename has no extension, load looks for a file named filename.mat. If filename has an extension other than .mat, load treats the file as ASCII data.

ASCII files must contain a rectangular table of numbers, with an equal number of elements in each row. The file delimiter (the character between elements in each row) can be a blank, comma, semicolon, or tab character. The file can contain MATLAB® comments (lines that begin with a percent sign, %).

Example: 'myFile.mat'

Names of variables, specified as one or more character vectors or string scalars. Each variable name must be a compile-time constant. Use the * wildcard to match patterns.

Example: coder.load('myFile.mat','A*') loads all variables in the file whose names start with A.

Regular expressions indicating which variables to load specified as one or more character vectors or string scalars. Each regular expression must be a compile-time constant.

Example: coder.load('myFile.mat', '-regexp', '^A') loads only variables whose names begin with A.

Output Arguments

collapse all

If filename is a MAT-file, S is a structure array.

If filename is an ASCII file, S is an m-by-n array of type double. m is the number of lines in the file and n is the number of values on a line.

Limitations

  • Arguments to coder.load must be compile-time constants.

  • The output S must be the name of a structure or array without any subscripting. For example, S(i) = coder.load('myFile.mat') is not allowed.

  • You cannot use save to save workspace data to a file inside a function intended for code generation. The code generator does not support the save function. Furthermore, you cannot use coder.extrinsic with save. Prior to generating code, you can use save to save workspace data to a file.

Tips

  • coder.load(filename) loads data at compile time, not at run time. If you change the content of filename after you generate code, the change is not reflected in the behavior of the generated code. If you are generating MEX code or code for Simulink® simulation, you can use the MATLAB function load to load run-time values.

  • If the MAT-file contains unsupported constructs, use coder.load(filename,var1,...,varN) to load only the supported constructs.

  • If you generate code in a MATLAB Coder™ project, the code generator practices incremental code generation for the coder.load function. When the MAT-file or ASCII file used by coder.load changes, the software rebuilds the code.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2013a