Change "current block"/gcb/gcbh programmatically

23 次查看(过去 30 天)
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 个评论
Thomas Becker
Thomas Becker 2016-9-15
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(' ');

请先登录,再进行评论。

采纳的回答

Swarooph
Swarooph 2016-8-12
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 个评论
Thomas Becker
Thomas Becker 2016-9-15
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...

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2016-8-12
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 个评论
Thomas Becker
Thomas Becker 2016-9-15
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by