Simulink coder with in-run configurable

9 vues (au cours des 30 derniers jours)
bac bac
bac bac le 23 Mar 2023
Hello,
I would like to write a program in Simulink and generate C code for Embedded system (or Desktop).
It is required the program to change parameters in-run.
For example:
Name program is: my_simulink_program
Parameters are: param_1, param_2, param_3, or file file_path hold those parameters
In cmd (of Windows or embedded OS) I need to pass p1, p2 for the parameters. It looks like:
> my_simulink_program param_1 p1 param_2 p2 param_3 p3
> my_simulink_program config_file=file_path
How can I do it in Simulink and Simulink coder, embedded coder?

Réponses (1)

Harsh
Harsh le 7 Mai 2025
You can create and deploy a MATLAB script with Simulink Compiler to take command line inputs and then use them in your Simulink Model. Here's an example Simulink model which takes two inputs and computes there product. We'll write a MATLAB script to deploy this model.
1. Create a Simulink Model -
  • Use two "Inport" blocks to take external input.
  • Connect the "Inport" blocks to a "Product" block which computes the product. Rename the "Inports" as "u1" and "u2" respectively.
  • Connect the output of the "Product" block to an "Outport" block
2. Write a MATLAB script to be deployed with Simulink Compiler. Some important points to consider while writing script are -
  • Convert the input to "double" as when you run the executable file from command line and pass the inputs they are intrepreted as a "string" object.
  • Use the "Simulink.SimulationData.Dataset" object to pass the external input data to model. For more details please refer to the following documentation - https://www.mathworks.com/help/simulink/slref/simulink.simulationdata.dataset.html
  • Configure the model for deplyment using "simulink.compiler.configureForDeployment" function.
Here's the complete script for the above model -
function multiply_script(u1_val, u2_val)
% Convert input strings to numeric values
u1_val = str2double(u1_val);
u2_val = str2double(u2_val);
% Define the model name
mdl = 'multiplyInputs';
simIn = Simulink.SimulationInput(mdl);
T = [0; 1];
ts1 = timeseries(u1_val * ones(size(T)), T);
ts2 = timeseries(u2_val * ones(size(T)), T);
ds = Simulink.SimulationData.Dataset;
ds = ds.addElement(ts1, 'u1');
ds = ds.addElement(ts2, 'u2');
% Set the external input using the Dataset
simIn = simIn.setExternalInput(ds);
% --- configure for deployment ---
simIn = simulink.compiler.configureForDeployment(simIn);
out = sim(simIn);
sig = out.yout{1};
ts = sig.Values;
finalProduct = ts.Data(end);
fprintf('The product is: %g\n', finalProduct);
end
3. Here's the output when you run the executable from terminal -
./multiply_script 2 6
The product is: 12
Note that if you need to use the embedded code then you need to modify the generated code to read data. Please refer to the following MATLAB answer for more details regarding this - https://www.mathworks.com/matlabcentral/answers/340391-how-do-i-generate-an-executable-file-from-simulink-which-reads-data-from-a-binary-file-to-use-those
I hope this resolves your query !

Catégories

En savoir plus sur Simulink Coder dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by