Convert Code Containing Structures to Fixed Point
This example shows how to convert a MATLAB® algorithm containing structures to fixed point.
In a local writable folder, create the function
struct_fcn.mfunction [out, y] = struct_fcn(in) % create a nested struct z = struct('prop1', struct('subprop1', 0, 'subprop2', [3 4 45])); % copy over the struct y = z; y.prop1.subprop1 = y.prop1.subprop1 + in; out = y.prop1.subprop1; end
In the same folder, create a test file,
struct_fcn_tb.m, that calls the function.for ii = 1:10 struct_fcn(ii); end
At the command line, create a
coder.FixPtConfigobject, and specify the test bench asstruct_fcn_tb.m.fixptcfg = coder.config('fixpt'); fixptcfg.TestBenchName = 'struct_fcn_tb';
Generate fixed-point code for
struct_fcn.mSince no input types are specified, the fixed-point conversion process simulates the test bench to infer types. To specify input types, use thefiaccel -float2fixed fixptcfg struct_fcn
-argsoption when you enter thefiaccelcommand.The automated fixed-point conversion process uses the test bench to simulate the function, gather range information, and propose data types. After fixed-point conversion is complete, in the command window, click struct_fcn_report.html to inspect the fixed-point data types.
When the names, number, and types of fields of two or more structures match, automated fixed-point conversion proposes a unified type. In this example, the range of
z.prop1.subprop1is[0,0], while the range ofy.prop1.subprop1is[0,10]. The app proposes a data type ofnumerictype(0,4,0)for bothz.prop1.subprop1andy.prop1.subprop1based on the union of the ranges of the two fields.
Click View Report to see the project summary details and links to the fixed-point MATLAB code and conversion report.