Main Content

View and Run Program Code Generated by SimBiology Model Analyzer

You can generate MATLAB® code for analysis programs in SimBiology Model Analyzer and run the code at the command line. The following example shows how to generate code for a simulation program, export the necessary inputs for the program to the MATLAB workspace, and run the code.

Open Bioavailability Project

The Bioavailability.sbproj file contains a simulation program that you can open directly in the Model Analyzer app. For details about the model, see Estimate the Bioavailability of a Drug.

At the MATLAB command line, enter:

simBiologyModelAnalyzer("Bioavailability.sbproj");

Generate Code for Simulation Program

  1. In the Browser panel, double-click Simulation to open the simulation program. Note that your panel can look slightly different than the screen shot below if you are in the Workspace view of the browser.

    Browser panel showing a list of project contents, including highlighted Simulation program

    The program shows the details about the simulation, such as the model, the dose being used, and simulation stop time.

  2. In the Browser panel, right-click Simulation and select View Program Code.

    The dropdown menu of Simulation program showing several context menu options, including View Program Code

  3. It opens an untitled MATLAB function file containing the code for the program. Save the file to a local directory in your computer using the default file name (runprogram.m).

    untitled MATLAB script file is shown with some MATLAB code

Export Input Arguments

Before you can run the generated code, you need to export the input arguments needed for the program code to your MATLAB workspace.

In the Browser panel, right-click Simulation and select Export Arguments for Program Code.

Use the variable name args for the program arguments.

Run Program Code and Plot Results

  1. In the MATLAB workspace, go to the directory where you saved runprogram.m. One way is to right-click the file name in the MATLAB Editor and select Change Current Folder to.

    Context menu of the function file showing option to change the current folder to the file location

  2. Run the generated code.

    programResults = runprogram(args{:})
    programResults = 
    
      struct with fields:
    
         input: [1×1 struct]
        output: [1×1 struct]
    

    The returned output programResults is a structure with two fields: input and output. The input contains a structure with the fields — model, cs (simulation settings), variants, and doses — that were used to run the program. The output contains a structure with the simulation results.

  3. Plot the simulation results.

    sbioplot(programResults.output.results)

    Time plot showing drug concentration in blood plasma and GI compartment

See Also

Related Topics