coder.load

Load compile-time constants from MAT-file or ASCII file into caller workspace

Syntax

  • S = coder.load(filename) example
  • S = coder.load(filename,var1,...,varN)
  • S = coder.load(filename,'-regexp',expr1,...,exprN)
  • S = coder.load(filename,'-ascii')
  • S = coder.load(filename,'-mat')
  • S = coder.load(filename,'-mat',var1,...,varN)
  • S = coder.load(filename,'-mat','-regexp', expr1,...,exprN)

Description

example

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.

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

expand all

Load compile-time constants from MAT-file

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.

Load compile-time constants from ASCII file

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

expand all

filename — Name of filestring

Name of file, specified as a string 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'

Data Types: char

var1,...,varN — Names of variables to loadstring

Names of variables, specified as string constants. Use the * wildcard to match patterns.

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

Data Types: char

expr1,...,exprN — Regular expressions indicating which variables to loadstring

Regular expressions indicating which variables to load, specified as string constants.

Example: load('myFile.mat', '^A', '^B') loads only variables whose names begin with A or B.

Data Types: char

Output Arguments

expand all

S — Loaded variables or datastructure array | m-by-n array

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

  • coder.load does not support loading objects.

  • Arguments to coder.load must be compile-time constant strings.

  • 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 generation software 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.

More About

expand all

Tips

  • coder.load loads data at compile time, not at run time. 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 generation software 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.

See Also

| |

Was this topic helpful?