How do I concatenate my output data from multiple simulations of my model? I have a model that I need to run in a loop for a certain number of times. The issue I am having is that I am unsure how to put the results of the previous run together with those of the current run into one data set. Is there an easy way to do this?
Prashant Kumar answered .
2025-11-20
The solution to this involves the monte carlo capabilities available using the command line with your Simulink model and the use of the VERTCAT function to concatenate the data after each run. The following example code uses the vdp demo model to demonstrate. For more information on the VERTCAT function, please type the following at the MATLAB command window prompt:
help vertcat
PLEASE NOTE: You first need to edit the vdp model by going to the Simulation menu and choosing Simulation Parameters-->Workspace I/O pane. Then check the Time and Output boxes.
% Initialize start and stop time as well as the time and output
start = 0;
stop = 10;
time = [];
output = [];
for i = 1:10
% Simulate the model and save the data by concatenating it
[t,state,y] = sim('test_vdp',[start stop]);
start = stop;
stop = stop + 10;
% Concatenate the previous data with the new data
% This will cause the end point to overlap (repeat), but is harmless
% as you can tell from the results (data is the same)
time = vertcat(time,t);
output = vertcat(output,y);
end
plot(time,output)
% Clear the data from the script
clear i start stop t y state
For more information on running a simulation from the command line, please refer to the Simulink documentation opened when you enter the following command at the MATLAB command prompt:
web([docroot,'/toolbox/simulink/ug/simulation28.html'])