Simulink: How do I change the value of a gain block with code?
4 views (last 30 days)
Show older comments
I would like to change the value of a gain block with Matlab code. I tried using the ModelWorkspace along with assignin, but the problem with this method is that I cannot generate C code from it.
What is the best way to modify a variable gain block and still be able to generate C code from the Simulink block diagram?
Thank you.
5 Comments
Accepted Answer
Robert
on 10 Aug 2016
From our conversation in the comments:
- You have 50+ gain blocks scaling signals in your model.
- You intend to generate C code from that model.
- You want the gain blocks to have time-varying gain – similar in behavior to a multiplication block but without the added clutter of extra signals.
- You were able to modify the gain values "on-the-fly" using an embedded MATALB function that used assignin to alter the gain value stored in the model workspace.
Were you designing this model before implementing it, I would recommend you use multiplication blocks instead of gain blocks. Now that your model is implemented and the 50+ gain blocks are in place, it might be excusable to bend the usual modeling best-practices and use GoTo and From tags with global scope to make a subsystem that uses a multiplication block but fits neatly in the spaces you now have gain blocks (one input, one output). To accomplish this, you would need to:
- Add a global goto tag to your control signal
- Create a subsystem in a library with one inport and one outport
- Add a multiplication block and a from tag to your subsystem to multiply your input by the (now variable) gain
- Use replace_blocks to swap out all the relevant gain blocks with your new pseudo-gain
You could replace the blocks by hand, in which case you don't strictly need a library, but with 50+ blocks to replace – and possible change in the future – you would be better off using a library and replace_block.
I haven't used global GoTo tags in code generation before but I don't see why they wouldn't be supported and noting in the documentation (that I have found) indicates that they aren't).
0 Comments
More Answers (0)
See Also
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!