Modifying a Simulink model mask from a Matlab app, built in App Designer.

5 ビュー (過去 30 日間)
Paul
Paul 2024 年 2 月 3 日
編集済み: Paul 2024 年 2 月 20 日
In Simulink, I have built a subsystem block with a mask, which includes a 'popup' mask entry, allowing the user to set a parameter to 'on' or 'off'. I now want to write to this parameter from a Matlab app, using, for example, the simulink.compiler.modifyParameters command, not the set_param command, because I want to deploy the app.
In the documentation for simulink.compiler.modifyParameters, examples are given to modify an 'edit' parameter in a mask, e.g.:
if time==5.0
% Modify global variables used by top model gain block
newGlobalVars = [Simulink.Simulation.Variable('gNum',1.1),...
Simulink.Simulation.Variable('gDen',0.5)];
simulink.compiler.modifyParameters(model,newGlobalVars);
end
However, this won't work for a 'popup' mask entry. How can I use simulink.compiler.modifyParameters to achieve this? For example, would:
parameterPath = [modelName '/' subsystemName '/' parameterName]
newParam_popup = struct(parameterPath, 'off');
simulink.compiler.modifyParameters(model, newParam_popup);
work i.e. writing directly to the mask path, not requiring the parameter to be set as a global or model workspace variable?
Thanks for any help provided!

採用された回答

Ayush Singh
Ayush Singh 2024 年 2 月 13 日
Hi Paul
As per the use of 'simulink.compiler.modifyParameters' , it does not directly modify mask parameters by specifying the block path and parameter name in the way you have described.
Instead, the function is designed to modify variables in the base workspace, model workspace, or as global parameters that are then linked to block parameters within the model.
Try using the following code to modify the 'popup' mask entry
% Name of Simulink model
modelName = 'your_model_name';
% Name variable linked to your mask parameter
parameterName = 'popupVar';
% The new value you want to set ('on' or 'off')
newValue = 'off';
% Create a Simulink.Simulation.Variable object with the new value
newParamVar = Simulink.Simulation.Variable(parameterName, newValue);
% Modify the parameter using simulink.compiler.modifyParameters
simulink.compiler.modifyParameters(modelName, newParamVar);
I hope the above information helps.
  1 件のコメント
Paul
Paul 2024 年 2 月 20 日
編集済み: Paul 2024 年 2 月 20 日
Hi Ayush,
Thanks for the response.
In the end I replaced mask 'popup' entries with 'edit' entries i.e. so my app could write to scalar values and not strings, which works fine with the 'simulink.compiler.modifyParameters' command i.e. as shown in the example: https://uk.mathworks.com/help/slcompiler/ref/simulink.compiler.modifyparameters.html
I believe however, there is a bug when trying to write vectors to a variable from an app, Again, as a workaround, these entries need to be broken down into scalars to allow 'simulink.compiler.modifyParameters' to work. The bug has been reported and I believe will be fixed in a future release.
Finally, if the variables are used to define transfer function coefficients, then compilation fails. The workaround here is to deconstruct the transfer function into a feedback loop using gain blocks and integrators, the allow scalar values to be written from an app.
Regards,
Paul.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubsystems についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by