Is it possible to automate a simulation with data from Excel in Simulink 4.1.2 (R12.1)?

5 views (last 30 days)
I have a complex model that I am trying to simulate using MATLAB 6.2 (R12.1) and Simulink 4.1.2 (R12.1). This model simulates different parameters. I have multiple combinationsfor the inputs and outputs in an Excel spreadsheet that need to be passed into the Simulink model for every simulation.
I would like to automate the process of sending one combination of input data after another from Excel to the Simulink model, and then read the output data from the model back to Excel after simulating a combination.
I would like to simulate the model for all the possible input data combinations present in the Excel sheet.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Nov 2009
In order to automate the process of sending one combination of input data after another from Excel to the Simulink model, and then read the output data from the model back to Excel after simulating a combination, a recommended approach would be to use ActiveX. For example, refer to the following template:
%open Excel via ActiveX
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
%open your existing workbook in Excel
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Open','PATH\YOUR_FILE.xls');
%make your sheet active (in this case, first sheet)
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
Activesheet = Excel.Activesheet;
%%%Begin loop
%capture the range and value of your parameter data in Excel
%NOTE: you can either try to capture all of the paramters for a simulation at once
%and then sort them in MATLAB or capture each parameter one by one
Range1 = get(Activesheet, 'Range', 'XX', 'YY');
val = Range1.value;
val = reshape([val{:}], size(val));
%set parameters for your model and run simulation from command line
set_param(MODELNAME, 'param1','val')
sim(MODELNAME)
%create StopFcn callback to save the simulated output data to workspace
%after simulation is run, capture range and place output data in Excel
Range2 = get(Activesheet, 'Range', 'XX2', 'YY2');
set(Range2, 'Value', output);
% save the workbook
invoke(Workbook, 'SaveAs', 'myfile.xls');
%%%End loop
% quit Excel
invoke(Excel, 'Quit');
Please note that the above is only a suggested template and will need to be modified by you to fit your particular application. In the above template, Excel is used as an ActiveX automation server and the parameters are set. The Simulink model is run from the MATLAB Command Prompt. For more information on MATLAB's ActiveX capabilities, refer to the link below.
<http://www.mathworks.com/support/tech-notes/2000/2001.html>
For more information and a more detailed example of how to use MATLAB and Excel, refer to the link below.
<http://www.mathworks.com/support/solutions/en/data/1-19BU0/index.html?solution=1-19BU0>
To learn more about setting parameters of your model and simulating it from the command line respectively,you can access the documentation by executing the following commands at the MATLAB Command Prompt.
doc set_param
doc sim

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!