Main Content

DataTypeWorkflow.FunctionReplacement

Object to store function replacements

Since R2025a

Description

The DataTypeWorkflow.FunctionReplacement object stores the custom function or lookup table replacements to use for unsupported functions in MATLAB Function blocks in the data type conversion workflow using DataTypeWorkflow.Converter. This object also contains the object functions entries, insert, lookup, and remove needed to manage function replacements.

Creation

Description

functionReplacement = DataTypeWorkflow.FunctionReplacement() creates a functionReplacement object in which to store function replacements to use for unsupported functions in MATLAB Function blocks.

example

Object Functions

entriesList all registered function replacements
insertRegister a new function replacement
lookupLook up a registered function replacement
removeRemove registered function replacement

Examples

collapse all

This example shows how to replace a function in a MATLAB Function block with a lookup table approximation and convert the system under design to use fixed-point data types.

When working with MATLAB Function blocks, some functions and MATLAB language constructs are not supported for the automated fixed-point conversion workflows. To work around unsupported functions:

  1. Use the getUnsupportedFunctionsForSystem function to identify functions used by MATLAB function blocks in the system under design which are not supported for automated fixed-point conversion using the DataTypeWorkflow.Converter object at the command line.

  2. Use the DataTypeWorkflow.FunctionReplacement object to specify custom function or lookup table replacements for unsupported functions. You can also use the DataTypeWorkflow.FunctionReplacement object to specify replacements for any function with a more efficient implementation.

  3. Use the applyDataTypes method of the converter object to apply the function replacements to MATLAB Function blocks in the system under design and proposed fixed-point data types to the model.

You can also use the DataTypeWorkflow.FunctionReplacement object to specify replacements for any function with a more efficient implementation.

Identify Unsupported Functions in MATLAB Function Blocks in System Under Design

Open the ex_mySinh model.

model = 'ex_mySinh';
open_system(model);

Create a DataTypeWorkflow.Converter object for the system under design, SubsystemA, which contains the MATLAB Function block of interest.

sud = [model '/SubsystemA'];
converter = DataTypeWorkflow.Converter(sud);

Use the getUnsupportedFunctionsForSystem function to identify unsupported functions used by MATLAB Function blocks in the system under design.

unsupportedFunctions = getUnsupportedFunctionsForSystem(converter)
unsupportedFunctions = 1×1 cell array
    {'sinh'}

The system under design contains a MATLAB Function block which computes the hyperbolic sine of the input. The sinh function is not supported for fixed-point conversion.

function y = my_sinh(u)
%#codegen
y = sinh(u);

Generate Optimized Lookup Table Approximation for Unsupported Function

Use the FunctionApproximation.compressLookupTables object to generate an optimized lookup table replacement for the sinh function.

Create a FunctionApproximation.Options object and set the ApproximateSolutionType to 'MATLAB' to output the optimized lookup table as a MATLAB function.

options = FunctionApproximation.Options();
options.ApproximateSolutionType = 'MATLAB';

Create a FunctionApproximation.Problem object to define the approximation problem.

functionToApproximate = 'sinh';
problem = FunctionApproximation.Problem(functionToApproximate,'Options',options);

Specify additional constraints on the function approximation problem.

problem.InputLowerBounds = 0;
problem.InputUpperBounds = 0.25;

Use the solve method to solve the optimization problem. The software displays the iterations of the optimization process.

solution = solve(problem);
Searching for fixed-point solutions.

|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |             64 |        1 |          2 |              16 |           16 |             EvenSpacing |     7.812500e-03, 1.675454e-03 |
|   1 |             64 |        1 |          2 |              16 |           16 |         EvenPow2Spacing |     7.812500e-03, 1.675454e-03 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|   1 |             64 |        1 |          2 |              16 |           16 |         EvenPow2Spacing |     7.812500e-03, 1.675454e-03 |

Save the best solution in the current working directory.

filename = 'sinhApproximate';
filepath = cd;
approximate(solution,'Name',filename,'Path',filepath);

Proposed Fixed-Point Data Types for System Under Design

Simulate the model and store the results in a run titled InitialRun.

converter.CurrentRunName = 'InitialRun';
converter.simulateSystem();

Collect ranges for the model. Use a shortcut to override the system with double-precision data types and enable instrumentation.

shortcuts = converter.ShortcutsForSelectedSystem;
converter.applySettingsFromShortcut(shortcuts{1});
converter.simulateSystem();

Propose data types for the system.

baselineRun = converter.CurrentRunName;
propSettings = DataTypeWorkflow.ProposalSettings;
converter.proposeDataTypes(baselineRun, propSettings);

Register Function Replacements

Create a DataTypeWorkflow.FunctionReplacement object to manage custom function replacements for the converter object.

functionReplacement = converter.FunctionReplacement();

Use the insert method to register a function replacement. For this example, replace the unsupported sinh function in the MATLAB Function block with the lookup table approximation, sinhApproximate.

functionReplacement.insert('sinh','sinhApproximate');

Use the entries method to display a list of all registered function replacements.

functionReplacement.entries()
ans=1×2 table
     Key            Value      
    ______    _________________

    "sinh"    "sinhApproximate"

Use the lookup method to look up a registered function replacement for a particular function.

functionReplacement.lookup('sinh')
ans = 
"sinhApproximate"

You can use the remove method to remove registered function replacements.

functionReplacement.remove('sinh');

Apply Function Replacements and Proposed Data Types

Use the applyDataTypes method to apply the registered function replacements to MATLAB Function blocks in the system under design and apply proposed fixed-point data types to the model.

converter.applyDataTypes(baselineRun);

Version History

Introduced in R2025a