generateCode
Syntax
Description
generateCode(
creates a MATLAB® function that recreates modelObj
)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(___,
specifies additional options using one or more name-value arguments.Name=Value
)
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.modelCode
= generateCode(___)
Examples
Generate Code for SimBiology Model
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.
generateCode(modelObj);
Generate code for properties that are set to default values.
generateCode(modelObj,IncludeDefaultValues=true);
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.
generateCode(modelObj,"radiodecayModel");
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 R2024b %% Create the model. m1 = sbiomodel("RadioactiveDecay"); m1.Notes = compose("<body xmlns=""http://www.w3.org/1999/xhtml"">\n <notes>\n <body xmlns=""http://www.w3.org/1999/xhtml"">\n <h1>Isomerization (Radioactive Decay) demo</h1>\n Reference:\n Daniel T. Gillespie, 1977, "Exact Stochastic Simulation of\n Coupled Chemical Reactions,"\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; end "
Input Arguments
modelObj
— SimBiology model
Model
object
SimBiology model, specified as a Model
object.
fileName
— Name of MATLAB function file
character vector | string scalar
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.
IncludeComments
— Flag to include comments
true
or 1 (default) | false
or 0
Flag to include comments for the generated MATLAB code, specified as a numeric or
logical 1 (true
) or 0 (false
).
Data Types: double
| string
IncludeConfigset
— Flag to generate code for active configset
true
or 1 (default) | false
or 0
Flag to generate code for the active SimBiology.Configset
, specified as a numeric or logical 1
(true
) or 0 (false
).
Data Types: double
| logical
IncludeDefaultValues
— Flag to generate code for properties with default values
false
or 0 (default) | true
or 1
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
IncludeRelease
— Flag to include MATLAB release information
true
or 1 (default) | false
or 0
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
OpenEditor
— Flag to show generated code in MATLAB editor
"auto"
(default) | false
or 0 | true
or 1
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
OverwriteFile
— Flag to overwrite existing file
false
or 0 (default) | true
or 1
Flag to overwrite the existing file, specified as a numeric or logical 1
(true
) or 0 (false
).
Data Types: double
| logical
Output Arguments
modelCode
— Generated MATLAB function
string scalar
Generated MATLAB function, returned as a string scalar.
Version History
Introduced in R2024a
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)