How to connect value in app designer to simulink

3 次查看(过去 30 天)
I have Simscape Simulink Model. I set the parameter of Spring Stiffness is k, Mass is m.
How to add value from Appdesign and Simulink ?
Thank you!

回答(1 个)

Samay Sagar
Samay Sagar 2024-8-20
Hi Thai,
I understand that you want to pass values for certain parameters (spring stiffness and mass) from a MLAPP to a Simulink model. Here is how you can create such an app:
  1. Create an MLAPP with input fields for “k” and “m”. These could be numeric edit fields or sliders, depending on your preference.
  2. Add a button to the app that, when clicked, will run the simulation with the specified parameters.
  3. Create a callback function for the button to update the parameters using “set_param”. Here is an example of how you might implement this:
function UpdateParametersButtonPushed(app, event)
% Retrieve parameter values from the app
k_value = app.SpringStiffnessEditField.Value;
m_value = app.MassEditField.Value;
% Set the parameters in the Simulink model
% Replace 'YourSpringBlock' and 'YourMassBlock' with the actual block paths
set_param('spring_mass_model/YourSpringBlock', 'Stiffness', num2str(k_value));
set_param('spring_mass_model/YourMassBlock', 'Mass', num2str(m_value));
% Run the simulation
simOut = sim('spring_mass_model', 'SimulationMode', 'normal');
% Extract the results if needed
time = simOut.tout;
outputs = simOut.yout{1}.Values.Data;
end
Alternatively, you can also configure your model to take the parameter values from the base workspace. In the above callback function, you can use “assignin” to update the corresponding variable as follows:
assignin('base', 'mass', m_value);
Ensure that the corresponding blocks take these values as variables from the workspace by specifying the same names in the block parameters.
If you want to pass input signals to your model from your app, you can refer the following documentation to prepare your input data:
For more information about “set_param” and “assignin”,you can refer the following documentation:

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by