Main Content

Generate Code forMATLABHandle Classes and System Objects

This example shows how to generate code for a user-defined System object™ and then view the generated code in the code generation report.

  1. In a writable folder, create a System object,AddOne, which subclasses frommatlab.System. Save the code asAddOne.m.

    classdefAddOne < matlab.System% ADDONE Compute an output value that increments the input by onemethods(Access=protected)% stepImpl method is called by the step methodfunctiony = stepImpl(~,x) y = x+1;endendend
  2. Write a function that uses this System object.

    functiony = testAddOne(x)%#codegenp = AddOne(); y = p.step(x);end
  3. Generate a MEX function for this code.

    codegen-reporttestAddOne-args{0}

    The-reportoption instructscodegento generate a code generation report, even if no errors or warnings occur. The-argsoption specifies that thetestAddOnefunction takes one scalar double input.

  4. Click theView reportlink.

  5. In theMATLAB苏rcepane, clicktestAddOne. To see information about the variables intestAddOne, click theVariablestab.

    This shows the MATLAB function testAddOne in the report. The cursor points to the variable p and the properties display.

  6. To view the class definition foraddOne, in theMATLAB苏rcepane, clickAddOne.

    This image shows the class definition for addOne in the report.

Related Topics