How can I change block parameters in simulink using block path in app designer?

I want to change block parameters (by refrrencing the block's path) in simulink using app designer.
In order to make .exe file I used SimulationInput object as the below code:
simInp = Simulink.SimulationInput('test_model');
I can't use simInp.setBlockParameter because of this error:
and I couldn't use set_param because it dosen't accept SimulationInput object an an input.
Here is the full code:
function startupFcn(app)
start_simulink;
end
% Button pushed function: simulateButton
function simulateButtonPushed(app, event)
try
simInp = Simulink.SimulationInput('test_model');
simInp = simInp.setBlockParameter("test_model/Gain",'Gain',num2str(app.kkEditField.Value));
% set_param('test_model/Gain','Gain',num2str(app.kkEditField.Value));
simInp = simulink.compiler.configureForDeployment(simInp);
simOut = sim(simInp);
t = simOut.tout;
y = simOut.yout{1}.Values.Data;
plot(app.UIAxes,t,y);
catch ME
errordlg(ME.message);
end
end
Would you please help me about the solution?

답변 (1개)

Hi Sabereh,
I understand that you are getting an error while trying to change a block parameter in Simulink using the SimulationInput object in App Designer.
This error happens because you are using setBlockParameter along with simulink.compiler.configureForDeployment. These two are not compatible — once you configure a simulation for deployment, you are not allowed to modify block parameters directly using setBlockParameter.
To resolve this, you can change the block parameter in your model (e.g., Gain value) to be a workspace variable, and then use setVariable instead of setBlockParameter to assign the value.
Here is the corrected code:
function simulateButtonPushed(app, event)
try
simInp = Simulink.SimulationInput('test_model');
simInp = simInp.setVariable('kk', app.kkEditField.Value); % Set gain value using variable
simInp = simulink.compiler.configureForDeployment(simInp); % Prepares for EXE
simOut = sim(simInp); % Run simulation
t = simOut.tout;
y = simOut.yout{1}.Values.Data;
plot(app.UIAxes, t, y); % Plot output
catch ME
errordlg(ME.message);
end
end
To apply this solution, ensure that the Gain block in your model has its value set to kk(just a variable name) instead of a fixed number. The line setVariable('kk', value) passes this value to the model correctly even when it is compiled into a standalone .exe.
For further reference, please see the following documentation:

댓글 수: 1

Hi Riya,
I'm looking to do something similar to pass a custom file name to the "record" block from the app so the user can select where to store model outputs. Documentation suggests you can only change the filename by modifying the block parameter directly, but this doesn't seem to be possible in the app designer. Is there any way to achive this?
Thanks,
Finlay

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Model, Block, and Port Callbacks에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 9월 6일

댓글:

2025년 8월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by