Looking for approach to import XLS data for code generation

3 views (last 30 days)
I have XLS which contains signal names used in simulink model. With details of initial value, data type, minimum maximum value etc.
I want to "import" this xls data and load into Model Explorer . After compilation it should be possible to generate code as per signal details in xls file. Model will have the signal names present in xls file always.
Probably this is very known thing to simplify code generation. But i am not able to figure out how to do this ?
Any existing threads or solution with use of latest matlab features (below 2022b , 2017b prefered ) will be also a great help.

Answers (2)

Aravind
Aravind on 29 Jan 2025
Edited: Aravind on 29 Jan 2025
To import signal names and their associated properties from an Excel file into Simulink's Model Explorer, and then use them for code generation, you can follow these steps. This process typically involves reading the Excel data into MATLAB, creating corresponding Simulink signal objects, and configuring them in the model. Here's a general guide on how to achieve this:
  1. Read Excel Data: Use MATLAB functions like "readtable" to import signal details (e.g., names, initial values, data types) from an Excel file. You can find more information regarding this function at the following documentation page: https://www.mathworks.com/help/releases/R2022b/matlab/ref/readtable.html
  2. Create Signal Objects: Loop through the imported data to create "Simulink.Signal" objects for each signal. Set properties like initial value, data type, and min/max values. Refer to https://www.mathworks.com/help/releases/R2022b/simulink/slref/simulink.signal.html for more information on how to create and setup Simulink Signal objects.
  3. Assign to Workspace: Use the "assignin" to place these signal objects in the MATLAB base workspace, making them available for the Simulink model. More information about the "assignin" function can be found at https://www.mathworks.com/help/releases/R2022b/matlab/ref/assignin.html
  4. Configure Model: Ensure your Simulink model references these signals correctly. The model should use the signal objects during simulation and code generation. You can do this by using the Model explorer to ensure that the signal names match those in the XLS file.
After this, you should be able to generate code as per your requirements. Through this process you can automate the integration of signal data into a Simulink model, simplifying code generation and ensuring consistency between your model and the Excel specifications.
Hope this helps!
  1 Comment
Thisis Idea
Thisis Idea on 16 Feb 2025
I accept this answer given with clear directions. But I am looking for the solution without using workspace. May be something like ..XLS to SLDD.

Sign in to comment.


Kanishk
Kanishk on 29 Jan 2025
You can use simple MATLAB script to import data from an Excel spreadsheet and utilize it as parameters in your Simulink model. Here is a simple script that demonstrates how to read data from an Excel file and create "Simulink.Parameter" objects. These parameters can be used within your Simulink model.
filename = 'signalData.xlsx';
signalData = readtable(filename);
for i = 1:height(signalData)
param = Simulink.Parameter;
signalName = signalData.SignalName{i};
param.Value = signalData.InitialValue(i);
param.DataType = signalData.DataType{i};
param.Min = signalData.MinimumValue(i);
param.Max = signalData.MaximumValue(i);
assignin('base', signalName, param);
end
To ensure that the data is imported every time the model is executed, you can add the above MATLAB code to the model callbacks, specifically in the "InitFcn" callback. This can be done by "Model Properties" > "Callbacks" > "InitFcn".
By default, the generated code will have parameters inlined. These parameters can be made tunable, by adjusting the "Default Parameter Behavior" setting in the code generation configuration:
  • Open the "Configuration Parameters" dialog in your Simulink model.
  • Navigate to "Code Generation" > "Optimization".
  • Set the "Default Parameter Behavior" to "Tunable".
This adjustment will define the parameter values in a separate file (e.g., <model_name>_data.c). You can further customize data import by writing a custom C file to import data from the spreadsheet and utilize the values in the generated files.
To access the documentation for "Simulink.Parameter" in MATLAB R2017b, you can use this command in MATLAB command window.
web(fullfile(docroot, 'simulink/slref/parameter.html'))

Categories

Find more on Reporting and Database Access in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!