Troubleshoot Range Analysis of System Objects

When deriving ranges for a model that uses a system object, the analysis fails if the model contains variables that can refer to multiple handle objects. The following example shows how to reconfigure the code so that the Fixed-Point Tool can derive ranges for the model.

In this example, range analysis of the first model ex_HandleVariableRefersToMultipleObjects produces an error because there is a variable in the code that can refer to different system objects depending on other conditions. The model, ex_HandleVariableRefersToSingleObject is a rewrite of the first model with the same functionality, but the Fixed-Point Tool is able to derive ranges for the model.

  1. Open the first model. At the MATLAB® command line, enter:

    addpath(fullfile(docroot,'toolbox','fixpoint','examples'))
    ex_HandleVariableRefersToMultipleObjects
    

    The code inside the MATLAB Function block refers to the custom System Object fAddConstant.

    function y  = fcn(u, c)
    %#codegen
    
    persistent hSysObjAddTen
    persistent hSysObjAddNegTen
    persistent hSysObjForStep
    
    if isempty(hSysObjAddTen)
        hSysObjAddTen = fAddConstant(10);
    end
    
    if isempty(hSysObjAddNegTen)
        hSysObjAddNegTen = fAddConstant(-10);
    end
    
    if c > 0
        hSysObjForStep = hSysObjAddTen;
    else
        hSysObjForStep = hSysObjAddNegTen;
    end
    
    y = step(hSysObjForStep, u);

  2. From the Simulink® Analysis menu, select Data Type Design > Fixed-Point Tool.

  3. In the Fixed-Point Tool, in the Prepare System section of the toolstrip, under System under design, select ex_HandleVariableRefersToMultipleObjects as the system you want to convert.

  4. In the Collect Ranges section of the toolstrip, select Derived Ranges.

  5. Click the Collect Ranges button.

    The analysis fails because there is a handle variable in the code that can refer to different system objects depending on the value of c.

  6. You can alter the code inside the MATLAB Function block so that the Fixed-Point Tool is able to derive ranges for the System Object by rewriting the code like the following:

    function y  = fcn(u, c)
    %#codegen
    
    persistent hSysObjAddTen
    persistent hSysObjAddNegTen
    
    if isempty(hSysObjAddTen)
        hSysObjAddTen = fAddConstant(10);
    end
    if isempty(hSysObjAddNegTen)
        hSysObjAddNegTen = fAddConstant(-10);
    end
    
    if c > 0
        y = step(hSysObjAddTen, u);    
    else
        y = step(hSysObjAddNegTen, u);    
    end
    
  7. Close the Fixed-Point Tool and the ex_HandleVariableRefersToMultipleObjects model. Open the ex_HandleVariableRefersToSingleObject model, which contains the rewritten code. At the MATLAB command line, enter:

    ex_HandleVariableRefersToSingleObject

  8. From the Simulink Analysis menu, select Data Type Design > Fixed-Point Tool.

  9. In the Collect Ranges section of the toolstrip, select Derived Ranges.

  10. Click the Collect Ranges button.

    This time, the Fixed-Point Tool successfully derives ranges for the variables used in the model.