Main Content


Generate MATLAB function to recreate SimBiology model

Since R2024a



generateCode(modelObj) creates a MATLAB® function that recreates modelObj, a SimBiology model, and opens an untitled MATLAB function file containing the function definition in the MATLAB editor. By default, the generated function is called createModel.


generateCode(modelObj,fileName) specifies the function file name fileName.


generateCode(___,Name=Value) specifies additional options using one or more name-value arguments.

modelCode = generateCode(___) returns the generated MATLAB function as a textual output. If you do not specify an output argument, the function does not return anything at the command line, that is, no ans variable is returned.


collapse all

Load the radio decay model.

modelObj = sbmlimport("radiodecay.xml");

Generate the MATLAB® code that recreates the model. By default, the function opens the MATLAB® editor and shows the generated code in an untitled MATLAB® function file.


Generate code for properties that are set to default values.


Specify the file name to save the generated code. By default, the file is created and saved in the current directory. The file extension (.m) is automatically added.


You can also return the generated code as a textual output by specifying an output argument.

modelCode = generateCode(modelObj)
modelCode = 
    "function m1 = createModel()
     % Created in MATLAB R2024a
     %% Create the model.
     m1 = sbiomodel("RadioactiveDecay");
     m1.Notes = compose("<body xmlns="""">\n  <notes>\n    <body xmlns="""">\n      <h1>Isomerization (Radioactive Decay) demo</h1>\n            Reference:\n            Daniel T. Gillespie, 1977, &quot;Exact Stochastic Simulation of\n            Coupled Chemical Reactions,&quot;\n            <i>The Journal of Physical Chemistry</i>, vol. 81, no. 25,\n            pp. 2340-2361.\n		</body>\n  </notes>\n</body>");
     %% Create compartments.
     c1 = addcompartment(m1, "unnamed");
     c1.Value = 1e-15;
     c1.Units = "litre";
     %% Create species.
     s1 = addspecies(c1, "x");
     s1.Value = 1000;
     s1.Units = "molecule";
     s2 = addspecies(c1, "z");
     s2.Units = "molecule";
     %% Create reactions.
     r1 = addreaction(m1, "x -> z");
     r1.ReactionRate = "c*x";
     r1.Name = "Reaction1";
     klaw1 = addkineticlaw(r1, 'Unknown');
     %% Create parameters.
     p1 = addparameter(klaw1, "c");
     p1.Value = 0.5;
     p1.Units = "1/second";
     %% Create and configure configsets.
     cs1 = getconfigset(m1, "default");
     cs1.CompileOptions.DimensionalAnalysis = false;

Input Arguments

collapse all

SimBiology model, specified as a Model object.

Name of the MATLAB function file, specified as a character vector or string scalar. Specify a file name or a full or relative file path with the file name. The function automatically adds the file extension (.m) as needed.

By default, the function creates the file in the current directory if you do not specify the full or relative file path.

Data Types: char | string

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: generateCode(modelObj,"myFcn.m",IncludeConfigset=true) generates myFcn.m in the current directory and the file includes code also to create the active configset and solver options.

Flag to include comments for the generated MATLAB code, specified as a numeric or logical 1 (true) or 0 (false).

Data Types: double | string

Flag to generate code for the active SimBiology.Configset, specified as a numeric or logical 1 (true) or 0 (false).

Data Types: double | logical

Flag to generate code for model component properties with default values, specified as a numeric or logical 1 (true) or 0 (false).

Data Types: double | logical

Flag to include the MATLAB release information as a comment, specified as a numeric or logical 1 (true) or 0 (false). The release corresponds to the MATLAB release that generates the code, such as R2024a.

Data Types: double | logical

Flag to show the generated code in the MATLAB editor, specified as "auto", true (1), or false (0). The default ("auto") option is to show the generated code in the MATLAB editor only if you do not specify the file name as the second input.

Data Types: double | logical | char | string

Flag to overwrite the existing file, specified as a numeric or logical 1 (true) or 0 (false).

Data Types: double | logical

Output Arguments

collapse all

Generated MATLAB function, returned as a string scalar.

Version History

Introduced in R2024a

See Also