Using Simulink.Parameter with MATLAB Runtime

7 views (last 30 days)
lithium_sulfate
lithium_sulfate on 11 Mar 2021
Answered: Divyam on 29 Aug 2024
I am tasked with the deployment of a SimuLink model which makes use of SimuLink.Parameter objects defined in MATLAB code. The parameter values are populated at runtime from a .mat file which is generated externally prior to running the script. When the script is run, it is supposed to load the file, populate the parameter values, run the simulation and then write its results back to a .mat file. The MATLAB script looks similar to this minimal example:
function runSimulation()
% Load data with simulation parameters
load(fullfile(getcwd(), 'params.mat'), 'sim_params');
% Populate simulation parameter values with external data
SimParams.Namespace1.length = Simulink.Parameter;
SimParams.Namespace1.length.Value = sim_params.length;
SimParams.Namespace1.length.DocUnits = 'm';
SimParams.Namespace1.duration = Simulink.Parameter;
SimParams.Namespace1.duration.Value = sim_params.duration;
SimParams.Namespace1.duration.DocUnits = 's';
% ... and so on
% Prepare simulation
in = Simulink.SimulationInput('Model');
in = in.setModelParameter('StartTime', '0');
in = in.setModelParameter('StopTime', 'sim_params.simulation_time');
in = in.setModelParameter('FixedStep', 'sim_params.simulation_step');
in = simulink.compiler.configureForDeployment(in);
% Run simulation
out = sim(in);
% Save results
result = out.result;
save(fullfile(getcwd(), 'result.mat'), 'result');
end
When run locally within the MATLAB editor environment, the script and model work as expected.
I ran into an issue when trying to deploy this script and model for execution on a headless Linux server using the MATLAB runtime. Compilation using MATLAB Compiler/SimuLink Compiler works fine. However, the generated application will not run. I receive the following error:
Unable to resolve the name Simulink.Parameter.
Error in runSimulation (line 6)
MATLAB:undefinedVarOrClass
As I understand, according to Support for MATLAB, Simulink, and Toolboxes, the only interfaces/functions supported by SimuLink Compiler are sim, Simulink.SimulationInput and Simulink.SimulationOutput, while Simulink.Parameter is not mentioned as supported.
I would like to know how I can make this script work with the MATLAB runtime; ideally with minimal to no modifications to the model itself, as it is not my work (again, I am merely tasked with deployment). Is the model as it is now categorically considered incompatible with the MATLAB runtime and SimuLink Compiler due to its dependence on Simulink.Parameter? Is the use of Simulink.Parameter generally discouraged due to these compatibility issues? Are there any alternatives to consider?
  1 Comment
Marc
Marc on 24 May 2023
I am facing the same issue at the moment. It is sad that there is no answer from the Mathworks support after more than two years!

Sign in to comment.

Answers (1)

Divyam
Divyam on 29 Aug 2024
As you correctly pointed out, the error "Unable to resolve the name Simulink.Parameter" persists because "Simulink.Parameter" methods are not supported with Simulink Compiler. Hence, even though you are using MATLAB runtime, you are unable to create or modify the "Simulink.Parameter" in your script.
A model which uses "Simulink.Parameter" can still be compiled on external deployments with MATLAB runtime if you create the "Simulink.Parameter" objects before the model is deployed and then modify the parameters using the "setVariable" method of the "Simulink.SimulationInput" object.
To avoid further errors, try not to edit the "Simulink.Parameter" objects inside the deployed script and replace the locations where your 'Simulink.Parameter' objects are used in the script with simple MATLAB variables.

Categories

Find more on Simulink Coder 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!