Main Content

Debug aMATLABFunction in a Chart

Check MATLAB Functions for Syntax Errors

Before you can build a simulation application for a model, you must fix syntax errors. Follow these steps to check the MATLAB®functionmeanstatsfor syntax violations:

  1. Open the functionmeanstatsinside the chart in thecall_stats_function_stateflowmodel that you constructed inProgram a MATLAB Function in a Chart.

    编辑器会自动检查你的函数代码for errors and recommends corrections.

  2. In theAppstab, clickSimulink Coder. In theC Codetab, clickBuild.

    If there are no errors or warnings, the Builder window appears and reports success. Otherwise, it lists errors. For example, if you change the name of local functionavgto a nonexistent local functionauginmeanstats, errors appear in the Diagnostic Viewer.

  3. The diagnostic message provides details of the type of error and a link to the code where the error occurred. The diagnostic message also contains a link to a diagnostic report that provides links to your MATLAB functions and compile-time type information for the variables and expressions in these functions. If your model fails to build, this information simplifies finding sources of error messages and aids understanding of type propagation rules. For more information about this report, seeMATLAB Function Reports(Simulink).

  4. In the diagnostic message, click the link after the function namemeanstatsto display the offending line of code.

    The offending line appears highlighted in the editor.

  5. Correct the error by changingaugback toavgand recompile. No errors are found and the compile completes successfully.

Run-Time Debugging for MATLAB Functions in Charts

You use simulation to test your MATLAB functions for run-time errors that are not detectable by Stateflow®. When you simulate your model, your MATLAB functions undergo diagnostic tests for missing or undefined information and possible logical conflicts as described inCheck MATLAB Functions for Syntax Errors. If no errors are found, the simulation of your model begins.

Follow these steps to simulate and debug themeanstatsfunction during run-time conditions:

  1. In the function editor, click the line number of this line:

    len = length(vals);

    The line number is highlighted in red, indicating that you have set a breakpoint.

  2. Start simulation for the model.

    If you get any errors or warnings, make corrections before you try to simulate again. Otherwise, simulation pauses when execution reaches the breakpoint you set. The line of code is highlighted, indicating this pause.

  3. To advance execution to the next line, selectStep Over.

    Notice that this line calls the local functionavg. If you selectStep Overhere, execution advances past the execution of the local functionavg. To track execution of the lines in the local functionavg, selectStep Ininstead.

  4. To advance execution to the first line of the called local functionavg, selectStep In.

    Once you are in the local function, you can advance through one line at a time with theStep Overtool. If the local function calls another local function, use theStep Intool to step into it. To continue through the remaining lines of the local function and go back to the line after the local function call, selectStep Out.

  5. SelectStep Overto execute the only line inavg.

  6. SelectStep Overto return to the functionmeanstats.

    Execution advances to the line after the call toavg.

  7. To display the value of the variablelen, point to the textlenin the function editor for at least a second.

    The value oflenappears adjacent to your pointer.

    You can display the value for any data in the MATLAB function in this way, no matter where it appears in the function. For example, you can display the values for the vectorvalsby placing your pointer over it as an argument to the functionlength, or as an argument in the function header.

    You can also report the values for MATLAB function data in the MATLAB Command Window during simulation. When you reach a breakpoint, thedebug>>command prompt appears in the MATLAB Command Window (you may have to pressEnterto see it). At this prompt, you can inspect data defined for the function by entering the name of the data, as shown in this example:

    debug>> len
    len = 4

    As another debugging alternative, you can display the execution result of a function line by omitting the terminating semicolon. If you do, execution results appear in the MATLAB Command Window during simulation.

  8. 离开,直到它又被称为函数the breakpoint is reached, selectContinue.

    At any point in a function, you can advance through the execution of the remaining lines of the function with theContinuetool. If you are at the end of the function, selectingStep Overcompletes the same action.

  9. Click the breakpoint to remove it and clickStopto complete the simulation.

    In the model, the computed values ofmeanandstdevnow appear in the Display blocks.

    Simulink model with display blocks showing the simulation results.

Check for Data Range Violations

To control the level of diagnostic action for data range violations, open the Configuration Parameters dialog box and, in theDiagnostics>Data Validitypane, set theSimulation range checkingparameter tonone,warning, orerror. The default setting isnone. For more information, seeSimulation range checking(Simulink).

Specify a Range

To specify a range for input and output data, follow these steps:

  1. In the Model Explorer, select the MATLAB function input or output of interest.

    The Data properties dialog box opens in theDialogpane of the Model Explorer.

  2. In the Data properties dialog box, click theGeneraltab and enter a limit range, as described inLimit range.