import data from excel into an s-function
Show older comments
I need to write a Matlab script that reads inputs and their type from a an excel file an create a new simulink model and add inputs and define their type
Answers (1)
Tejas
on 27 Dec 2024
Hello Fairouz,
To read data from an Excel file, the 'readtable' function can be used. Below is a syntax on how to utilize this function. More information on the function, can be found in the documentation: https://www.mathworks.com/help/matlab/ref/readtable.html
filename = 'inputs.xlsx';
inputData = readtable(filename);
For creating a Simulink model with input blocks and modifying their parameters using a MATLAB script, here is an example to guide you through the process. In this example, the input from the Excel file includes the names for the 'Constant' blocks, their values, and their data types.
- Start by creating a Simulink model.
modelName = 'AutoGeneratedModel';
new_system(modelName);
open_system(modelName);
- Iterate over input data from the Excel file and add 'Constant' blocks to the model using the 'add_block' function. More information about this function can be found in the documentation: https://www.mathworks.com/help/simulink/slref/add_block.html .
- To set the name and data type of each 'Constant' block, use the 'set_param' function. More information about this function can be found in the documentation: https://www.mathworks.com/help/simulink/slref/set_param.html
for i = 1:height(inputData)
inputName = inputData.InputName{i};
dataType = inputData.DataType{i};
value = num2str(inputData.Value(i));
blockName = ['Constant', num2str(i)];
blockPath = [modelName, '/', blockName];
add_block('simulink/Sources/Constant', blockPath, 'MakeNameUnique', 'on');
set_param(blockPath, 'Name', inputName);
set_param(blockPath, 'OutDataTypeStr', dataType);
set_param(blockPath, 'Value', value);
end
- Re-arrange the blocks and save the model.
Simulink.BlockDiagram.arrangeSystem(modelName);
save_system(modelName);
close_system(modelName);
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!