If a FIR filter block (or any other Xilinx block) is using variables from a work space, you might want to load the variables (e.g., filter coefficients) from a file so they are loaded every time the model is opened.
You can use the "set_param()" Simulink function to associate system (model) parameters with the Simulink model. The "set_param() function" is described in the Simulink User's Guide.
If you want to associate pre-load behavior with your model, use the "PreLoadFcn" system parameter. If you want to perform post-processing, use the "StopFcn" parameter. A list of system parameters are included in the Appendix of the Simulink User's Guide.
The following resolutions describe the following:
- How to use purely MATLAB-based functions/parameters
- How to use MATLAB functions/parameters with the Xilinx-supplied "XLREQUIRE", including an explanation of what XLREQUIRE does and the benefits of using it
NOTE: There are "PreLoadFcn" steps in the demo design examples.
Using Purely MATLAB-based Functions/Parameters
NOTE: Step 1 is an example of how to create a ".m" file, and it explains what this file might contain.
1. Create a MATLAB ".m" file that contains the coefficients required and any other values needed for the model. MATLAB files should be saved as a ".m" file. A simple example of a ".m" file named "coef.m" is illustrated below:
Coef = [ -0.0024
Bit_width = 10;
2. Set the Simulink model "PreLoadFcn" parameter to call this file, and load the variables into the MATLAB workspace. When the PreLoadFcn parameter is set in Simulink, the value of the parameter is executed each time the Simulink model is opened. The following illustrates what is typed in the MATLAB console in a generic format:
In the above example, "OBJ" is a system or block path name; this sets the specified parameters to the specified values. Value strings are case-sensitive; parameter names are not case-sensitive. Parameters that correspond to dialog box entries have string values. The syntax used for the attached example is:
set_param('fir_design', 'PreLoadFcn', 'coef;')
3. In the FIR filter block in the Simulink model, specify the "coef" variable in the filter coefficient section, and specify "bit_width" in block parameters where required. These values stored in the coef and bit_width variables are loaded into the work space by "PreLoadFcn".
Using MATLAB Functions and XLREQUIRE
NOTE: For more information on usage of XLREQUIRE, enter "help xlrequire" in the MATLAB command prompt. The MATLAB path on Xilinx System Generator is set up to present XLREQUIRE help and to find the Xilinx demos.
Q: What does XLREQUIRE do and what are the benefits of using it?
A: XLREQUIRE prepends the model name and an underscore to the argument supplied so this example requires "coef.m" to be located and executed before the model is loaded.
The two benefits of xlrequire() are that looks for m-code scripts (e.g., preload) in the directory where the model resides (which might not be the working directory), and it does a better job of catching and reporting errors. Xilinx created xlrequire() to use in Xilinx demo designs so that the pre/post function calls are more robust.
Use the Simulink "set_param()" function to associate system (model) parameters with your Simulink model. For example, if you want the file "fir_design_coef.m" to run before opening the integrate model, open the model, and at the MATLAB console, enter:
set_param ( 'fir_design' , 'PreLoadFcn' , 'xlrequire (''coef.m''); ')
NOTE: The two quotation marks on either side of "coef.m" are single quotation marks without spaces between them (rather than one double quotation mark.)
The XLREQUIRE function resides in the System Generator demo directory. Any functions written by you can be used in the "set_param" command if they are in your MATLAB path. Xlrequire() prepends the model name and an underscore to the input argument. Consequently, in the example above, xlrequire (''coef.m'') causes the execution of the coef.m file (as the model name is "fir_design.m").
For more information on using XLREQUIRE, enter "help xlrequire" at the MATLAB command prompt.
The following is from the XLREQUIRE Help in System Generator v2.1:
>> help xlrequire
XLREQUIRE locates and loads m-scripts and (.mat) data files associated with an MDL. XLREQUIRE makes this process less haphazard and opaque.
Example Usage 1:
When invoked from an .mdl file named "foo.mdl", xlrequire("PreLoadFcn") or xlrequire("PreLoadFcn.m") attempts to find a file named "foo_PreLoadFcn.m" in the same directory from which "foo.mdl" was launched and invoke that script. If the m-script cannot be located, or produces an error, the problem is reported to the MATLAB console.
Example Usage 2:
When invoked from an .mdl file named "foo.mdl", xlrequire("MyData.mat") attempts to find a file named "foo_MyData.mat" in the same directory from which "foo.mdl" was launched, and load the data it contains into (base) workspace variables.
You can hook a "PreLoad" callback function to an .mdl file with the following command:
> set_param( bdroot, 'PreLoadFcn', 'xlrequire(''PreLoadFcn'');');
The simplest way to specify the Preloadfcn, stopfcn, and over model parameter values is as follows:
1. Right click on the model and select model properties to open a dialog box.
2. Select the Callback tab.
3. Specify the MATLAB commands for each function of the model.