I want to create a Simscape model from a MATLAB script. How can I add blocks, set their parameters, and connect physical ports?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2026 年 11 月 17 日 0:00
編集済み: MathWorks Support Team 2026 年 11 月 18 日 13:02

0 投票

Use the MATLAB and Simulink programmatic modeling functions to create a model, add blocks, and set block parameters. For physical connections, use the "simscape.addConnection" function in R2025a and later, or the "add_line" function with physical port handles in earlier releases.
The following example requires MATLAB, Simulink, and Simscape. It creates a circuit with an AC voltage source, a resistor, an Electrical Reference block, and a Solver Configuration block. Use a model name that is not already in use.
First, create the model and add the blocks:
model = 'simpleSimscapeCircuit';
open_system(new_system(model));
resistor = add_block('fl_lib/Electrical/Electrical Elements/Resistor', ...
[model '/Resistor'], 'Position', [200 50 250 80]);
source = add_block('fl_lib/Electrical/Electrical Sources/AC Voltage Source', ...
[model '/Source'], 'Position', [50 50 100 80]);
reference = add_block('fl_lib/Electrical/Electrical Elements/Electrical Reference', ...
[model '/Reference'], 'Position', [170 150 210 180]);
solver = add_block('nesl_utility/Solver Configuration', ...
[model '/Solver'], 'Position', [270 150 310 180]);
set_param(resistor, 'R', '100');
set_param(source, 'amp', '5');
To find a block's library path, locate the block in the Simulink Library Browser and inspect its displayed path. Use the "get_param" function with "DialogParameters" to inspect the parameters available for a block:
get_param(resistor, 'DialogParameters')
Connect the blocks in R2025a and later
Use the "simscape.addConnection" function with block handles or paths and the names of the ports to connect:
simscape.addConnection(source, 'p', resistor, 'p');
simscape.addConnection(source, 'n', resistor, 'n');
simscape.addConnection(source, 'n', reference, 'V');
simscape.addConnection(source, 'n', solver, 'port');
Use the "simscape.connectionPortProperties" function to inspect a block's physical port names and types. Port names can differ from the labels displayed on the block:
simscape.connectionPortProperties(source)
Connect the blocks before R2025a
Use the "get_param" function with "PortHandles" to obtain the handles of the physical ports. Then use the "add_line" function to connect those ports. Run this section instead of the R2025a-and-later connection section:
sourcePorts = get_param(source, 'PortHandles');
resistorPorts = get_param(resistor, 'PortHandles');
referencePorts = get_param(reference, 'PortHandles');
solverPorts = get_param(solver, 'PortHandles');
add_line(model, sourcePorts.LConn(1), resistorPorts.LConn(1));
add_line(model, sourcePorts.RConn(1), resistorPorts.RConn(1));
add_line(model, sourcePorts.RConn(1), referencePorts.LConn(1));
add_line(model, sourcePorts.RConn(1), solverPorts.RConn(1));
"LConn" and "RConn" identify connection-port groups. Their indices depend on the block, so inspect each block's port handles and identify the ports before connecting them. Connect compatible ports, such as electrical conserving ports to other electrical conserving ports. Physical signal ports are distinct from conserving ports.
After connecting the blocks using either method, arrange the model diagram programmatically:
Simulink.BlockDiagram.arrangeSystem(model);
For more layout options, see Configure Model Layout Programmatically.
The completed circuit can look like the following image. Block positions and line routing can differ from the example script.
After using either connection section, simulate the model:
in = Simulink.SimulationInput(model);
in = in.setModelParameter('StopTime', '0.1');
out = sim(in);
This circuit has no output configured for viewing. To view a measurement, add a sensor, a PS-Simulink Converter block, and a Scope block. Use the "save_system" function to save the model.
For more information, see simscape.addConnection, add_line, add_block, get_param, and set_param.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Simulation Setup についてさらに検索

製品

リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by