How to change simulink variable from matlab script?

56 visualizaciones (últimos 30 días)
David Boelke
David Boelke el 10 de Oct. de 2022
Respondida: David Boelke el 21 de Oct. de 2022
This is a simplified example of the problem I am facing.
What I have:
I have a simulink model (PT1.slx):
And I have a matlab script (control.m):
% clean up
close all
% use simulink
% plot results
hold on
The script, can set the value of a constant, start the model and plot the output, neat!
What I want:
I want to be able to control the value inside my transfer function from my matlab script, for instance this:
What I have tried:
I have found out, that you can't control values like the one circled in red from datalines, for numeric reasons. But I can create a 'variable' for this value and control it from the 'model explorer', but how I can control my variables from a matlab script I do not know.
I have read through the 'set_param' help to see if it could be used for my purposes, but it seems I need a different tool.
I have searched around and found forum entries like
It talks about how someone can control 'simulink parameters', I don't think that is the same as a 'variable'.

Respuesta aceptada

Benjamin Thompson
Benjamin Thompson el 10 de Oct. de 2022
  1 comentario
David Boelke
David Boelke el 21 de Oct. de 2022
Thank you, that was the information I needed.

Iniciar sesión para comentar.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 10 de Oct. de 2022
Editada: Fangjun Jiang el 10 de Oct. de 2022
You can use set_param().
block='PT1/Transfer Fcn';
a =
'[1 1 0]'
b =
1 1 0
b =
1.0000 1.5000 0
c =
'[1 1.5 0]'
  1 comentario
David Boelke
David Boelke el 21 de Oct. de 2022
That is also a good solution, I used the model workspace, but yours is a good answer, as it is closer to what I used before, thank you.

Iniciar sesión para comentar.

David Boelke
David Boelke el 21 de Oct. de 2022
What I used is this:
Then I just replace the number in my simulink with the variable T. It is amazing, I can place variables either in the model workspace or in the base workspace, very cool.


Más información sobre Programmatic Model Editing en Help Center y File Exchange.




Community Treasure Hunt

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

Start Hunting!

Translated by