MATLAB Answers

How Do I Use a Structure as the Value in a Constant Block to Form a Bus?

35 views (last 30 days)
Paul
Paul on 22 Apr 2020
Answered: Daniel Luder on 24 Apr 2020
I have a Simulink model with a Constant block feeding a scope. The Contant Value parameter is set to foo, a struct defined this way:
>> foo.a = 1; foo.b = 2;
>> who
Your variables are:
foo
Updating the diagram yields an error in the diagnostic viewer:
The data type specified for the block 'untitled/Constant' is 'Inherit: Inherit from 'Constant value'' and the value specified for the parameter 'Value' of the block is a MATLAB structure. MATLAB structures are supported only when bus types are explicitly specified. Set the data type explicitly to a matching bus object or specify an expression which resolves to a bus object.
This makes sense to me. But note that final clause "specify an expression." I'll come back to this later.
I create a bus ojbect in the base workspace:
>> foobusinfo = Simulink.Bus.createObject(foo);whos
Name Size Bytes Class Attributes
foo 1x1 368 struct
foobusinfo 1x1 364 struct
slBus1 1x1 132 Simulink.Bus
Note that the function poofed a new variable into the mix, namely slBus1. I had no control over the name of the poofed variable, but I can find out what it is:
>> foobusinfo.busName
ans =
'slBus1'
Now I can go the Signal Attributes of the Constant block and specify the Output data type as: Bus: slBus1. Update the diagram and it works.
My problem is that I had to use slBus1 as hard coded parameter in that block, which means that every time from now on I have to have slBus1 in my workspace and it has to be consistent with the structure foo. But what if change foo? Then i have to recreate foobusinfo, which will in turn create a new Simulink.Bus object, maybe slBus5 or something, at which point my diagram won't work without editing it.
So, is there way to use the expression foobusinfo.busName in the Signal Attributes of the Constant block so that it's always using the current bus object that associates with foo, assuming that every time I change foo I update foobusinfo? Note that the diagnostic above suggests an expression can be used, but I haven't figure out how to do that. Better yet, is there a way just call Simulink.Bus.createObject(foo) somewhere in the Contstant blook parameters, or maybe a callback, so that I only need to worry about foo and then everything else adapts automatically?

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!