Connect MATLAB and RoadRunner to Control and Analyze Simulations
This topic shows you how to create and verify the connection between MATLAB® and RoadRunner. Once you create this connection, you can use MATLAB commands to:
- Open and close the RoadRunner application. 
- Open, close, and save scenes, scenarios, and projects. 
- Start, pause, and stop simulations. 
- Obtain and analyze data from simulation logs. 
- Import and export scenes and scenarios. 
Note
You must connect MATLAB and RoadRunner to simulate behaviors based in MATLAB- or Simulink® in RoadRunner Scenario.
This topic assumes you have:
- An Automated Driving Toolbox™ license. 
- A RoadRunner license, and the product is installed. For information on installing and activating RoadRunner, see Install and Activate RoadRunner (RoadRunner). 
- A RoadRunner Scenario license, and the product is installed. 
- A RoadRunner project folder. For information on creating a RoadRunner project, see RoadRunner Project and Scene System (RoadRunner). 
Set Up Environment to Launch RoadRunner from MATLAB
You must specify the install location of RoadRunner to MATLAB to ensure the roadrunner object opens RoadRunner successfully.
If you do not specify an installation directory, the roadrunner
            object opens RoadRunner from the default installation folder for the platform you are using,
            either Windows® or Linux®. These installation locations are the defaults by platform:
- Windows – - C:\Program Files\RoadRunner R20NNx\bin\win64
- Linux, Ubuntu® – - /usr/local/RoadRunner_R20NNx/bin/glnxa64
R20NNx is the MATLAB release you are using, such as R2023b. Use the same release of MATLAB and RoadRunner.
If your RoadRunner installation is in a different location than the default location, use
            these MATLAB commands to change the default value of the RoadRunner installation folder. Replace MyInstallationFolder with
            the full path to the folder containing the AppRoadRunner.exe
            executable.
Tip
You only need to set the RoadRunner installation folder once using these commands. The value you set persists between MATLAB sessions.
RRInstallationFolder = "MyInstallationFolder";
s = settings;
s.roadrunner.application.InstallationFolder.PersonalValue = RRInstallationFolder; 
s.roadrunner.application.InstallationFolder.TemporaryValue = RRInstallationFolder;Tip
If you run the commands in this topic in a MATLAB script, place pause statements between commands to
                allow RoadRunner sufficient time to respond. Pause for at least 0.5 seconds between
                commands for best results.
Launch RoadRunner Using MATLAB
You can use the roadrunner
            object to launch RoadRunner in an existing project. The roadrunner object provides
            functions for manipulating scenes and scenarios and managing simulations and simulation
            data. 
The roadrunner object requires an argument that specifies the
            location of an existing RoadRunner project. For more information on the RoadRunner project system and how to create a new project, see RoadRunner Project and Scene System (RoadRunner).
This code opens RoadRunner to a project located in C:\RR\MyProject and creates a
                roadrunner object called rrApp. Replace
                C:\RR\MyProject with the path to your project folder. 
rrProj = "C:\RR\MyProject";
rrApp = roadrunner(rrProj);Verify RoadRunner Launch
When you run this step successfully, you see these results:
- A new instance of the RoadRunner application opens to the specified project. 
- No error messages are displayed in the MATLAB Command Window. 
- The RoadRunner Output Window displays no errors and may display a message about starting the RoadRunner API server. 
To check the status of this connection, run this command.
status(rrApp)
status (RoadRunner).Open a Scenario in RoadRunner
After creating a roadrunner object, you can open an existing
            scenario in RoadRunner Scenario from MATLAB. To open a scenario, use the openScenario function, specifying the roadrunner
            object and the scenario filename that you want to open. For example, this code opens the
                TrajectoryCutIn scenario file, which is a scenario included by
            default in RoadRunner projects.
openScenario(rrApp, "TrajectoryCutIn.rrscenario");Verify Scenario Is Opened
When you run this step successfully, you see these results:
- RoadRunner opens to the specified scenario in scenario editing mode. 
- No error messages are displayed in the MATLAB Command Window. 
- The RoadRunner Output Window displays no errors and may display a message about starting the Simulation API server. 
Create a Scenario Simulation Object to Manage Simulations
After you open a scenario and switch to scenario editing mode, use the createSimulation function with the roadrunner object
            to create a ScenarioSimulation object. This object provides functions to start, stop,
            and pause simulations as well as to retrieve simulation logs containing actor and event
            data. For example, this code creates a scenario simulation object named
                rrSim.
rrSim = createSimulation(rrApp);
Tip
Run the createSimulation function only once when setting up the
                connection. To close and reopen RoadRunner during a MATLAB session, delete the simulation object first using this command before
                rerunning
                createSimulation.
delete(rrSim);
ScenarioSimulation
                objects.Verify Scenario Simulation Object
When you run this step successfully, you see these results:
- No error messages are displayed in the MATLAB Command Window. 
- No error messages are displayed in the RoadRunner Output Window. 
Start and Stop Simulation from MATLAB
Once you connect RoadRunner and MATLAB and created a scenarioSimulation object, you can start,
            stop and pause scenario simulation using these commands, where rrSim
            is replaced by the name of your scenarioSimulation object.
- Start the simulation. - set(rrSim, "SimulationCommand", "start") - Tip - This command may return while the simulation is still running. To pause MATLAB execution until the simulation finishes, use this code. This code checks the simulation status and executes a- set(rrSim,"SimulationCommand","Start"); while strcmp(get(rrSim,"SimulationStatus"),"Running") pause(1); end - pausecommand while the simulation is still running.
- Stop the simulation. - set(rrSim, "SimulationCommand", "stop") 
- Pause the simulation. - set(rrSim, "SimulationCommand", "pause") 
Next Steps for Scene Management and Scenario Simulation
Once you connect MATLAB and RoadRunner and create a roadrunner object, you can open, close,
            import and export scenes and scenarios programmatically. See roadrunner
            and Export Multiple Scenes Using MATLAB for more
            information.
Once you create a simulation object, you can:
- Control RoadRunner Scenario simulations from MATLAB and retrieve and analyze simulation logs such as actor pose and velocity. See Simulate a RoadRunner Scenario Using MATLAB Functions. 
- Simulate RoadRunner Scenario actors using behaviors modeled in MATLAB or Simulink. - For an overview, see Overview of Simulating RoadRunner Scenarios with MATLAB and Simulink. 
- For simulating a behavior modeled by a MATLAB System object™, see Simulate RoadRunner Scenarios with Actors Modeled in MATLAB. 
- For simulating a behavior modeled by a Simulink model, see Simulate RoadRunner Scenarios with Actors Modeled in Simulink. 
 
See Also
roadrunner | ScenarioSimulation | set | get | ActorSimulation | ScenarioLog