How can I click on “OK” or “Apply” button programmatically

16 views (last 30 days)
Since I did not get any respond regarding updating CAN Pack/Unpack modules, now I am thinking on a way to click on “OK” or “Apply” button of “Function Block Parameters: CAN Pack” programmatically. I can open and close it by open_system and close_system. The name of CAN message on CAN Pack model will be updated but the signals (ports) do not change. Any idea would be greatly appreciated. Thanks

Answers (3)

Swarooph
Swarooph on 8 Jul 2016
Any Simulink block can be programmatically set using the set_param function. Look here for documentation. The catch is you need to know exactly what that parameter is called for your particular block. You can do this using the following steps:
  1. Interactively select that particular block in your model
  2. Execute the following command on the MATLAB command prompt
>> get_param(gcb,'DialogParameters') %Before executing this command, the 'CAN Pack' block was selected
ans =
DataFormat: [1x1 struct]
CANdbFile: [1x1 struct]
MsgList: [1x1 struct]
MsgName: [1x1 struct]
MsgIDType: [1x1 struct]
MsgIdentifier: [1x1 struct]
MsgLength: [1x1 struct]
SignalInfo: [1x1 struct]
NSignals: [1x1 struct]
StartBits: [1x1 struct]
SignalSizes: [1x1 struct]
ByteOrders: [1x1 struct]
DataTypes: [1x1 struct]
MultiplexTypes: [1x1 struct]
MultiplexValues: [1x1 struct]
Factors: [1x1 struct]
Offsets: [1x1 struct]
Minimums: [1x1 struct]
Maximums: [1x1 struct]
Remote: [1x1 struct]
In your case the property name is MsgName. To set this using set_param, you would use the following syntax for example:
set_param('myModel/CAN Pack','MsgName','myMessage')
Again, refer to the documentation (linked above) for the full syntax or how it applies to other parameters. Note also that, when you do this, you don't have to worry about clicking 'OK' or 'Apply'.
  6 Comments
Christoph Hahn
Christoph Hahn on 12 Jul 2016
Is the answer you got from Swarooph helpful?
If not, please let us know why what you are missing.
Have you created an actual help request yet? - I mean here: http://www.mathworks.com/support/contact_us/
Cheers Christoph
Saeed Soltani
Saeed Soltani on 12 Jul 2016
Hi Christoph, The answer of Swarooph was not helpful, because as I have mentioned in the previous comment, I had already done this process but didn’t get expected answer. I will try to contact support team with the link you have given.
Thanks.

Sign in to comment.


Alberto Guiggiani
Alberto Guiggiani on 10 Jan 2017
Edited: Alberto Guiggiani on 10 Jan 2017
I've implemented a workaround for this problem. It's quite ugly, but it's the only way that I found so far to programmatically work with CAN Pack / Unpack blocks.
The trick is to use Java to emulate keypresses on the dialog box to change anything in it enabling the OK/Apply to refresh the signal list. For example (assuming that the "block_path" variable contains the path of the CAN block):
robot = java.awt.Robot;
open_system(block_path)
robot.keyPress(java.awt.event.KeyEvent.VK_TAB);
robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);
robot.keyPress(java.awt.event.KeyEvent.VK_TAB);
robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);
robot.keyPress(java.awt.event.KeyEvent.VK_RIGHT);
robot.keyRelease(java.awt.event.KeyEvent.VK_RIGHT);
robot.keyPress(java.awt.event.KeyEvent.VK_BACK_SPACE);
robot.keyRelease(java.awt.event.KeyEvent.VK_BACK_SPACE);
robot.keyPress(java.awt.event.KeyEvent.VK_C);
robot.keyRelease(java.awt.event.KeyEvent.VK_C);
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);
pause(0.1)
What it does is pressing TAB key twice to highlight the CANdb file textbox (assuming that you configured the block to "CANdb specified signals"), then deleting and writing again the "c" at the end of .dbc. This is detected as a dialog box update and causes a refresh of the signal list.
The above key sequence is just an example and can be replaced to something else that still causes the "Apply" button to become clickable. A full list of the key names can be found here .
  1 Comment
Muhammet Samil Ikizoglu
Muhammet Samil Ikizoglu on 12 Feb 2020
I was persist to handle the problem and thanks to your ugly response I overcame. Whether it is ugly or not, it works and appreciated with you. We do not always follow optimum solutions as you know.

Sign in to comment.


Jianfei
Jianfei on 27 Mar 2024
Set a 'Tag' value in the button, then use the following function to control the mouse to click this button
function click(figureName, tagValue)
hFig = findall(0,'tag',figureName);
hBtn = findobj(hFig, 'Tag', tagValue);
jbtn = findjobj(hBtn);
pause(0.1)
p = jbtn.getLocationOnScreen;
r = java.awt.Robot;
pause(0.5)
r.mouseMove(p.x + jbtn.getWidth / 2, p.y + jbtn.getHeight / 2);
pause(0.05)
r.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)
pause(0.1)
r.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
end

Categories

Find more on Event Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!