How to retrieve a subblock name at initalization.

Dear community, I want to create a subblock which returns the name of the subblock as a String. (The subblock name shall be used as a key to retrieved a value from a database and I want to make the access of the database visible in simulink).
Currently I use a masked subblock with a 'Parameters' variable 'MyName'. The subblock contains a constant, which returns the value of a variable 'MyName'. The variable 'MyName' shall be initialized with the name of the subblock it belongs to.
A code like MyName = get_param(gcb,'Name'); does not work in the Initialization box for the dialog variable 'MyName'.
Since the subblock name is static I guess Simulink provides something like 'this.Name', which is available at initialization time. Is this kind of function available?
Can anyone help?

 Risposta accettata

Hope the parameter with same name as block name is present in the base workspace whose value will be assigned to mask variable MyName in the Mask Initialization.
In mask initialization pane, write,
MyName = evalin('base',get_param(gcb,'Name'));

5 Commenti

Thany you for the answer,
unfortunately that does not work and creates an error
"Invalid setting in 'CodegenTest1/AccessName/Constant' for parameter 'Value'"
So I guess I cannot use get_param at all. Is there something available like a static function which returns the name of the subblock?
Thomas, block name can be accessed with get_param only. Any way few questions:
  1. what you have entered in Constant block's "Value" field ?
  2. Have you created the parameter on base workspace with the same name as block name. This parameter is supposed to provide the value to Constant block
Ok,
  1. I have entered 'MyName' for Constant value field
  2. I am not sure if I understand you question correctly. I have made in simulink File/New/Model, then I added a subblock. In the subblock I have added a constant connected to an output. The constant uses 'MyName' for Constant value field. Then I masked the subblock. Then I right click on the subblock and select edit mask. In Edit Mask, under tab Parameters I added the variable MyName. At least in tab Initialization for MyName I now want to initialize the variable MyName with the subblock name.
Your hint gave me the solution. If I put in the Initialization pane for MyName
MyName=cast(char(evalin('base',get_param(gcb,'Name'))), 'uint8');
then I get the Subsystem name as an output. Thank you for your help.
The shortest solution is:
MyName=cast(char(get_param(gcb,'Name')), 'uint8');

Accedi per commentare.

Più risposte (1)

Another simpler option might be to just use a constant block with the value:
uint8('LookupKey')
You could then hide the block name and expand the block so that value text is visible.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by