Change "current block"/gcb/gcbh programmatically

25 visualizzazioni (ultimi 30 giorni)
Hey MATLABers,
is there any way to change the current block in Simulink, which will be returned by gcb/gcbh? I would like to start GUIs programmatically, but their code relies on gcb (works perfectly, as long as you start a GUI by double-clickling).
Unfortunately, the following code does not make a block the current block:
>> set_param(hBlock,'Selected','on');
>> isequal(hBlock,gcbh)
ans =
0
Other possibility: programmatically trigger a mouse-click on a Simulink block, but I don't know how...
Thanks very much and best regards
Thomas
  1 Commento
Thomas Becker
Thomas Becker il 15 Set 2016
My own solution is here, using the commands:
set_param(0,'CurrentSystem',...)
set_param(gcs,'CurrentBlock',...)
Example:
% Open test model (R2014b)
clc
fuelsys;
% Set current subsystem ("compiled mode" necessary?)
set_param(0,'CurrentSystem','fuelsys/fuel rate controller');
% Set current block
set_param(gcs,'CurrentBlock','control logic');
% Get current block
disp('Current block:');
disp(gcb);
disp(' ');
% Selected block might be different
disp('Selected block:');
set_param('fuelsys/Metered Fuel','Selected','on');
disp(char(find_system('fuelsys','Selected','on')));
disp(' ');
% Get current block
disp('Current block:');
disp(gcb);
disp(' ');

Accedi per commentare.

Risposta accettata

Swarooph
Swarooph il 12 Ago 2016
Is it not possible to change the code to use block path (string such as 'myModel/Gain') instead of gcb or gcbh? This would be the most straightforward thing to try. If you cannot do this for various reasons:
How do you get the value of hBlock? If you are hard coding it as a variable (by specifying a number) there could be two issues with this:
  1. As this documentation says, You can use the block handle in subsequent calls to get_param or set_param. If you examine the handle, you can see that it contains a double. Do not try to use the number of a handle alone (e.g., 5.007) because you usually need to specify many more digits than MATLAB displays. Instead, assign the handle to a variable and use that variable name to specify a block. Use getSimulinkBlockHandle to get the handle to a block.
  2. Using hard coded values might result in breaking of logic since Simulink can change the handle to the block every time the model is loaded/opened. For e.g it could be 16.0001 now. If I close and open the model again, it maybe different.
  3. I am guessing the equality test might fail because you are trying to compare two floating point numbers.
For e.g. if I do this, it seems to work:
%Open demo model
vdp
%Set Mu block as selected
set_param('vdp/Mu','Selected','on')
%Get Handle to the block
hBlock = getSimulinkBlockHandle(gcb)
%Compare handle value to gcbh
isequal(hBlock,gcbh)
If you run this code mutliple times (each time clearing workspace and closing the open vdp model), you can see the value of hBlock keeps changing.
  1 Commento
Thomas Becker
Thomas Becker il 15 Set 2016
Thanks for your answer, Swarooph. Unfortunately it is not possible to change the code itself, although it would be the most straightforward solution, which I would prefer, too. I post my own solution here...

Accedi per commentare.

Più risposte (1)

Fangjun Jiang
Fangjun Jiang il 12 Ago 2016
The best way to resolve this is to use the actual block path or block handle, instead of gcb/gcbh. You could modify the function. At the beginning of the code, assign
CurrentBlock='ModelName/SubsystemName/BlockName';
CurrentBlockHandle=get_param(CurrentBlock,'Handle');
Then do a find&replace on gcb and gcbh.
According to R2015b document, there is many ways gcb/gcbh could change.
The current block is one of these:
During editing, the current block is the block most recently clicked.
During simulation of a system that contains S-Function blocks, the current block is the S-Function block currently executing its corresponding MATLAB® function.
During callbacks, the current block is the block whose callback routine is being executed.
During evaluation of the MaskInitialization string, the current block is the block whose mask is being evaluated.
Another quick and dirty way that I hesitate to suggest, is that if all those processing happen inside a function, you could create the variable gcb and gcbh to overwrite the function gcb and gcbh.
  1 Commento
Thomas Becker
Thomas Becker il 15 Set 2016
Thanks for your answer, Fangjun. Unfortunately it is not possible to change the code itself, although it would be the most straightforward solution, which I would prefer, too. I post my own solution here...

Accedi per commentare.

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by