Best practice for simulink variable sharing

조회 수: 1 (최근 30일)
Ced
Ced 2015년 10월 15일
This is less of a question and more of an opinion poll. It's about the best practice for sharing data needed in a simulink model but modified at runtime in an m-file.
So:
I have a simulink model with several blocks containing either gains or transfer functions which are defined in the workspace.
To do so, I can e.g. define some parameter.m file, and run it at the beginning of my script. So far so good, but I want to have the possibility to change some variables in my script "at runtime" and have them be updated in the simulink model, e.g. recompute the transfer functions with some new parameters. This computation is quite ugly, so I would like to have it as a function. Since there are quite a few variables, a nested function would be ideal, but here the problems start.
Options I can think of and why they don't work (for me):
a) have functions take all the parameters as input variables. Possible, but a pain to update if some parameters or so should be added. Also, can easily contain typos if a parameter is missed or swapped. I guess some ugly input parsing could be done, but for me that would be a complicated fix to an ugly solution :)
b) I cannot have a nested function inside a script, but I cannot make my script into a function since I then cannot do run('parameters.m') (or can I? couldn't figure it out)
c) I could define a parameter struct and just have it passed around. Clean solution for variables, but the simulink model becomes really ugly since all my variables are called parameters.A, parameters.B etc, so the math becomes impossible to check. Not an option.
d) I could have the parameters as global variables, in theory... but this is a bigger project, way too risky. Same for calling variables from random workspaces with evalin
e) I can define a function taking all the parameters and creating a struct, i.e. generate_struct_from_vars. Then I use this struct as variable to pass around between functions. I think that this is currently my favourite option, since it 1) avoids having to send lots of variables in variable calls, 2) keeps the function definitions constant even if some new parameters are added, 3) allows to update parameters in simulink, 4) does not use global variables. Nonetheless, one has to be a bit careful since there are basically two instances of each variable, one in the struct and one as "standalone variable", so one has to be careful when changing values.
Any better options? I'd love to hear your comments and suggestions. Thanks!

답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by