coder.load - Load constants from MAT file or ASCII file at code generation time - MATLAB (original) (raw)
Load constants from MAT file or ASCII file at code generation time
Syntax
Description
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename))
loads constant values from filename
.
- If
filename
is a MAT file, thencoder.load
loads variables from the MAT file into a structure array. - If
filename
is an ASCII file, thencoder.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 offilename
after you generate code, the change is not reflected in the behavior of the generated code.
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename),[var1,...,varN](#btucr5w-1-var1varN))
loads only the specified variables from the MAT filefilename
.
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename),`'-regexp'`,[expr1,...,exprN](#btucr5w-1-expr1exprN))
loads only the variables that match the specified regular expressions.
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename),`'-ascii'`)
treats filename
as an ASCII file, regardless of the file extension.
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename),`'-mat'`)
treats filename
as a MAT file, regardless of the file extension.
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename),`'-mat'`,[var1,...,varN](#btucr5w-1-var1varN))
treats filename
as a MAT file and loads only the specified variables from the file.
[S](#btucr5w-1-S) = coder.load([filename](#btucr5w-1-filename),`'-mat'`,`'-regexp'`, [expr1,...,exprN](#btucr5w-1-expr1exprN))
treats filename
as a MAT file and loads only the variables that match the specified regular expressions.
Examples
Generate code for a function edgeDetect1
that, given a normalized image, returns an image where the edges are detected with respect to the threshold value. edgeDetect1
usescoder.load
to load the edge detection kernel from a MAT file at code generation 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 thecodegen\lib\edgeDetect1
folder.
Generate code for a function edgeDetect2
that, given a normalized image, returns an image where the edges are detected with respect to the threshold value. edgeDetect2
usescoder.load
to load the edge detection kernel from an ASCII file at code generation 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 thecodegen\lib\edgeDetect2
folder.
Input Arguments
Name of file. filename
must be a constant at code generation time.
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 constant at code generation time. 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 constant at code generation time.
Example: coder.load('myFile.mat', '-regexp', '^A')
loads only variables whose names begin with A
.
Output Arguments
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 constants at code generation time. - 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 thesave
function. Furthermore, you cannot usecoder.extrinsic
withsave
. Prior to generating code, you can usesave
to save workspace data to a file.
Tips
coder.load(filename)
loads data at code generation 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 functionload
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 bycoder.load
changes, the software rebuilds the code.
Extended Capabilities
Version History
Introduced in R2013a