How to programmatically "click" the block mask Apply or OK button.

I have created a Block Mask with a Text area. A callback is writing a text to this Text area using set_param(gcb,'VariableText_', 'A little house');
The text is written into the Text Area and clicking the Apply button will save the text. That works Ok, off cause.
What I want to do, and is not able to figure out, is how I programmatically can "click" the Apply or the OK button from the callback?
The purpose is to save the contents of the Text area programmatically.
People have been asking this question before, but I have not found any useful answer.

7 commentaires

If you already have a callback to update the text area, can the callback be updated to save the text directly, instead of having to simulate clicking of the UI button ?
I cant attest to simulink as I dont have experience with it, but I have several matlab GUI's with text fields, which I grab user inputs from thusly;
%The comment edit box on the GUI:
CommentEditBox=uicontrol('Style','Edit','Units', 'normalized','string','No comment','Position',[0.075,0.815,0.145,0.02], 'backgroundcolor',[0.931 0.716 0.684],'CallBack', @CommentEditCallBack, 'HorizontalAlignment','center','visible','on','Parent', GUI);
%The edit function, which automatically fires when any input is given to the uicontrol.
function CommentEditCallBack(~,~)
Comment = get(CommentEditBox,'String');
%Optional: change the box color to green so you know something updated ;)
set(CommentEditBox, 'backgroundcolor',[0.731 0.916 0.684],'HorizontalAlignment','center');
drawnow()
end
I dont know if a similar approach will work in simulink?
Petter Gran-Jansen
Petter Gran-Jansen le 23 Jan 2020
Modifié(e) : Petter Gran-Jansen le 23 Jan 2020
Hi Mohammad
Thanks for your suggestion, but no luck :(
Hi Jakob
Thanks for your suggestion, but it doesn’t work for Simulink :(
It seems that the only way to save parameters in the mask is to click the Apply or OK button.
You may have to look outside of matlab to accomplish your goal. Matlab can interface with java, you can use java.awt.Robot to do the clicking. If you have multiple screens, you will need to know which screen has a popup. Also you will need to know the position of the buttons on the screen.
Try and get a handle for the simulink window. then you can find the button and its position on the screen. See this answer on how to get the handle. https://www.mathworks.com/matlabcentral/answers/183928-how-to-get-the-handle-of-simulink-window-in-matlab Worst case scenario you will need to do a screen capture and process it to identify the button location.
Note if you have a high res display and the windows 10 is scaling up the size, there may be differences in the position used in java.awt.Robot and matlab. you may have to trial and error.
Petter Gran-Jansen
Petter Gran-Jansen le 24 Jan 2020
Modifié(e) : Petter Gran-Jansen le 24 Jan 2020
Hi Mohammad
Thanks for your engagement trying to help me. I appreciate it.
Yes, I could use java.awt.Robot to do the clicking. But it is not an elegant solution. I am very surprised that it looks like Matlab doesn’t have any solution. I had expected Matlab to have a command like “clickMaskApply” , “clickMaskOK”, “saveMask” or similar to be used from a callback. I would say that there are basic commands.
Maybe someone from Matlab can contribute to this question, please?
A bit late but maybe this helps somebody:
selected_block_handle = get_param(gcb,'Handle');
dlgs = DAStudio.ToolRoot.getOpenDialogs;
for i=1:length(dlgs) %find dialog of selected block
if class(dlgs(i).getSource) == "Simulink.SLDialogSource"
dialog_block_handle = dlgs(i).getSource.getBlock.Handle;
if dialog_block_handle == selected_block_handle
dlgs(i).apply %'click' apply
end
end
end

Connectez-vous pour commenter.

Réponses (1)

Fangjun Jiang
Fangjun Jiang le 17 Sep 2020
I don't understand the problem.
If the block has a mask of a text area, when user opens the mask by double_clicking the block, the block dialog window opens, the user can type in any text, then, the user can click the "Apply" or "Ok" button, the text is saved with the block.
Running the command "set_param(gcb,'VariableText_', 'A little house')" will do the same. The text 'A little house' is saved with the block afterwards, without the need to worry about clicking the "Apply" or "Ok" button.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by