Main Content

Use the Fixed-Point Tool to Find and Replace Unsupported Functions in MATLAB Function Blocks

When working with MATLAB Function blocks, some functions and MATLAB® language constructs are not supported for the automated fixed-point conversion workflows, including the iterative fixed-point conversion workflow in the Fixed-Point Tool. For a list of supported and unsupported constructs, see MATLAB Language Features Supported for Automated Fixed-Point Conversion. In addition, some functions, such as exp and sinh, are not supported because they cannot take fixed-point inputs. For a successful conversion, replace or skip unsupported functions used by MATLAB Function blocks in the system under design.

To replace unsupported functions using a command-line workflow, use the getUnsupportedFunctionsForSystem function to find unsupported functions in the system under design and the DataTypeWorkflow.FunctionReplacement object to specify function replacements. For an example of the command-line workflow, see Replace Function in MATLAB Function Block with Lookup Table Approximation and Convert to Fixed Point.

Function Replacement in the Fixed-Point Tool

When your system under design contains MATLAB Function blocks, the Setup pane in the iterative fixed-point conversion workflow of the Fixed-Point Tool includes the Function Replacement section. Any unsupported functions the MATLAB Function blocks in the system under design use are listed in the table with options for replacement.

The Replacement column in the table provides these options:

  • Use default replacement — This option is available when a default function replacement is available. The default is a CORDIC-based implementation of the function.

  • Set up a lookup table — This option is available when the function is compatible with a lookup table replacement. Selecting this option launches the Lookup Table Optimizer app.

    After you complete the steps in the Lookup Table Optimizer to create a lookup table approximation for the unsupported function, the custom lookup table replacement is saved in the current working directory. Use the Refresh functions list option to make the lookup table approximation available in the drop-down menu, and then select the custom lookup table replacement from the list.

  • customFunction1, customFunction2, … — Custom functions in the current working directory are listed in the drop-down menu. To refresh this list when you add or remove functions from the current working directory, select Refresh functions list.

  • Refresh functions list — When you add additional custom functions or lookup table approximations to the current working directory, select this option to refresh the drop-down list.

  • Skip this step for now — Select this option to take no action on the unsupported function. The MATLAB Function Block Best Practices check in the preparation stage will flag this as a warning. You can use coder.float2fixed.skip inside the MATLAB Function block to exclude the unsupported function from conversion. If you take no action to exclude the unsupported function, fixed-point conversion fails.

Function replacements are applied to the model when you click the Apply Data Types button.

Note

When you use the Fixed-Point Tool to replace an unsupported function, the tool replaces all instances of the function in the system under design with the specified replacement.

Use the Fixed-Point Tool to Replace Unsupported sinh Function in MATLAB Function Block

This example shows how to use the Fixed-Point Tool to find and replace unsupported functions used inside a MATLAB Function block, and then convert the system under design to use fixed-point data types.

Open Model Containing MATLAB Function Block

Open the ex_mySinh model.

model = 'ex_mySinh';
open_system(model);

In this model, SubsystemA contains the MATLAB Function block my_sinh, which implements the hyperbolic sine function.

function y = my_sinh(u)

%#codegen

y = sinh(u);

This function does not support fixed-point data types, so it is not supported for automated fixed-point conversion workflows.

Replace Unsupported Function with Default Replacement

  1. Open the Fixed-Point Tool. On the Apps tab, select Fixed-Point Tool.

  2. In the tool, select the Iterative Fixed-Point Conversion workflow.

  3. In the System Under Design (SUD) section of the Setup pane, select SubsystemA as the system to convert.

  4. In the Function Replacement section, the table shows that the MATLAB Function block in the system under design contains the unsupported function sinh. In the table, select Use default replacement for this function.

    The Fixed-Point Tool is open to the Setup pane of the Iterative Fixed-Point Conversion workflow. The Function Replacement section is highlighted and the table lists sinh as an unupported MATLAB function in the system under design.

  5. Click Prepare.

    The MATLAB Function Block Best Practices check shows that with the default replacement enabled, all MATLAB Function blocks in the system under design are supported for fixed-point conversion.

  6. Complete the fixed-point conversion workflow using the default settings.

    1. Click Collect Ranges.

    2. Click Propose Data Types.

    3. Click Apply Data Types.

      When you apply the proposed data types to the system under design, the tool also applies replacements for unsupported functions.

  7. Verify the function replacement in the MATLAB Function block.

    1. Open SubsystemA in the ex_mySinh model. The MATLAB Function block in this subsystem is replaced with a variant subsystem. The active variant, MATLAB Function_FixPt, is the fixed-point compatible version of the function specified in the function replacement step.

      The MATLAB Function block in the ex_mySinh model shows the variant subsystem created by the function replacement.

    2. Open the MATLAB Function_FixPt block. The default function replacement option replaced the unsupported sinh function with a CORDIC-based approximation, cordicsinhcosh.

      The code in the MATLAB Function block shows that it now uses the cordicsinhcosh function.

Replace Unsupported Function with Lookup Table Approximation

  1. To restore the original version of the model, click Restore Original Model.

  2. In the tool, select the Iterative Fixed-Point Conversion workflow.

  3. In the System Under Design (SUD) section of the Setup pane, select SubsystemA as the system to convert.

  4. In the Function Replacement section, the table shows that the MATLAB Function block in the system under design contains the unsupported function sinh. In the table, select Set up a lookup table for this function. The Lookup Table Optimizer app opens.

    Use the Lookup Table Optimizer app with the default settings to set up a lookup table replacement for the sinh function:

    1. In the Lookup Table Optimizer app, the MATLAB Function Handle is @(x)sinh(x), which corresponds to the sinh function used by the MATLAB Function block.

    2. Click Next.

    3. Click Optimize.

    4. Click Next.

    5. Click Show Optimized LUT.

      The app creates a new function in the current working directory. The code in this new function is a lookup table approximation of the sinh function.

    6. Click Finish.

  5. In the Fixed-Point Tool Function Replacement table, select Refresh functions list from the drop down for the sinh function.

  6. From the drop down, select the custom function replacement you created using the Lookup Table Optimizer app.

  7. In the Fixed-Point Tool, click Prepare.

    The MATLAB Function Block Best Practices check shows that with the default replacement enabled, all MATLAB Function blocks in the system under design are supported for fixed-point conversion.

  8. Complete the fixed-point conversion workflow using the default settings.

    1. Click Collect Ranges.

    2. Click Propose Data Types.

    3. Click Apply Data Types.

      When you apply the proposed data types to the system under design, the tool also applies replacements for unsupported functions.

  9. Verify the function replacement in the MATLAB Function block.

    1. Open SubsystemA in the ex_mySinh model. The MATLAB Function block in this subsystem is replaced with a variant subsystem. The active variant, MATLAB Function_FixPt, is the fixed-point compatible version of the function specified in the function replacement step.

    2. Open the MATLAB Function_FixPt block. The tool replaced the unsupported sinh function with a lookup table approximation that you created using the Lookup Table Optimizer app.

      The code in the MATLAB Function block shows that it now uses the lookup table approximation you created using the Lookup Table Optimizer app.

See Also

| | |

Topics