| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → SimEvents |
| Contents | Index |
| Learn more about SimEvents |
| On this page… |
|---|
This example illustrates how to change values of attributes between rapid simulation runs. The example:
Modifies a model to make it possible to change attribute values.
Builds a rapid simulation executable and a MAT-file containing input data representing different attribute values.
Simulates the executable multiple times, using a different set of attribute values each time.
Plots the results.
Real-Time Workshop software license
Write access to the current folder in the MATLAB session
This example uses the Distributed Processing of Multi-Class Jobs demo model.
Modify the model to record a relevant simulation result. In this case, the result is the load of Service Station 1:
From the MATLAB Command Window, enter sedemo_star_routing to open the model.
In the current folder, save the model as star_routing_rsim.mdl.
Open the Scopes subsystem by double-clicking it.
From the SimEvents Sinks library, drag the Discrete Event Signal to Workspace block into the model.
Branch the line from the Load_Server1 block to the Signal Scope1 block. Connect the new branch to the Discrete Event Signal to Workspace block. The blocks look like this figure:

Save the model again.
Modify the model to accept varying values of attributes that represent job definitions:
In the Command Window, define variables called serviceprocess and servicetime:
serviceprocess = [1 2 4 2 3 5]; servicetime = [2 1 5 3 4 0];
Open the Multiple-Class Job Generation subsystem by double-clicking it.
Open the dialog box of the Set Attribute block labeled Class 1 job definition. In the A1 and A2 rows of the table, set Value From to Signal port.
From the Simulink Sources library, drag two copies of the Constant block into the subsystem.
Connect the Constant blocks to the A1 and A2 signal input ports of the Set Attribute block.
In the block dialog boxes of the Constant blocks, set Constant value to the values in the table.
| In the Constant Block that Connects to the... | Set Constant Value to... |
|---|---|
| A1 signal input port | serviceprocess |
| A2 signal input port | servicetime |
The blocks look like this figure:

Configure and build the model for rapid simulation:
From the model window, select Simulation > Configuration Parameters.
In the Configuration Parameters dialog box, on the Real-Time Workshop pane, set System target file to rsim.tlc. Click Apply.
In the Configuration Parameters dialog box, on the Real-Time Workshop RSim Target pane, select Enable RSim executable to load parameters from a MAT-file.
In the Configuration Parameters dialog box, on the Optimization pane, select Inline parameters. Click Configure.
In the Model Parameter Configuration dialog box, in the Source list section, select serviceprocess and servicetime. Click Add to table. Click OK.
In the Configuration Parameters dialog box, click OK.
From the model window, select Tools > Real-Time Workshop > Build Model.
From the Command Window, set up input data by creating and saving a MAT-file. The file contains parameter sets. Each parameter set designates a service process vector and a service time vector for one simulation run.
% Create a cell array with service process values in the first column,
% and corresponding service time values in the second column.
service_array = cell(3,2); % Preallocate space.
% Baseline parameter set
service_array(1,:) = {[1 2 4 2 3 5], [2 1 5 3 4 0]};
% Try swapping the roles of stations 1 and 3 in the service process.
service_array(2,:) = {[3 2 4 2 1 5], [4 1 5 3 2 0]};
% Try moving the role of station 3 to the beginning of the service process.
service_array(3,:) = {[3 1 2 4 2 5], [4 2 1 5 3 0]};
% Create a parameter structure to store the values.
rtP = rsimgetrtp('star_routing_rsim','AddTunableParamInfo','on');
rtP = rsimsetrtpparam(rtP,3);
for k = 1:3
rtP = rsimsetrtpparam(rtP,k,...
'serviceprocess',service_array{k,1},...
'servicetime',service_array{k,2});
end
save star_routing_rsim_service rtPFrom the Command Window, run the executable multiple times, using a different parameter set each time:
% Run the executable with different pairs of service process and % service time values. Store results in corresponding MAT-files. system(['star_routing_rsim -o star_routing_rsim_results1.mat '... '-p star_routing_rsim_service.mat@1']); system(['star_routing_rsim -o star_routing_rsim_results2.mat '... '-p star_routing_rsim_service.mat@2']); system(['star_routing_rsim -o star_routing_rsim_results3.mat '... '-p star_routing_rsim_service.mat@3']);
Alternative After setting appropriate environment variables, you can run the executable from a shell window instead of from the Command Window. For more information about environment variables to set, see Requirements for Running Rapid Simulations in the Real-Time Workshop documentation. |
Plot simulation results:
h = figure;
subplot(3,1,1);
load star_routing_rsim_results1;
stairs(rt_simout.time,rt_simout.signals.values);
title(['Load of Service Station 1 with ServiceProcess = [' ...
num2str(service_array{1,1}) ']']);
axis([0 100 0 4.5])
subplot(3,1,2);
load star_routing_rsim_results2;
stairs(rt_simout.time,rt_simout.signals.values);
title(['Load of Service Station 1 with ServiceProcess = [' ...
num2str(service_array{2,1}) ']']);
axis([0 100 0 4.5])
subplot(3,1,3);
load star_routing_rsim_results3;
stairs(rt_simout.time,rt_simout.signals.values);
title(['Load of Service Station 1 with ServiceProcess = [' ...
num2str(service_array{3,1}) ']']);
axis([0 100 0 4.5])

![]() | Example: Computing an Ensemble Average Using Rapid Simulation | Example: Varying Queue Capacity Between Runs Using Rapid Simulation | ![]() |

Learn how you can use SimEvents discrete-event simulation capbilities through these technical resources.
Get free kit| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |