Simulink: How do I change the value of a gain block with code?

15 ビュー (過去 30 日間)
J H
J H 2016 年 8 月 8 日
編集済み: Robert 2016 年 8 月 10 日
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 件のコメント
J H
J H 2016 年 8 月 9 日
Thank you.
Yes. I am trying to modify the gains while the model is running.
I was considering using a multiplication operator; however, I have several (50+) locations (within several subsystems) where the gains are used. Consequently, my model would become very messy if I had to route signals through multiplication operators instead of using variable gains.
Is there any other way that I could accomplish this task?
Thanks again.
Robert
Robert 2016 年 8 月 10 日
編集済み: Robert 2016 年 8 月 10 日
I think I understand your question now. I took a crack at answering it, below.

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

採用された回答

Robert
Robert 2016 年 8 月 10 日
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:
  1. Add a global goto tag to your control signal
  2. Create a subsystem in a library with one inport and one outport
  3. Add a multiplication block and a from tag to your subsystem to multiply your input by the (now variable) gain
  4. 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 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by