How to avoid clicking buttons in GUI with uiwait in a script
Afficher commentaires plus anciens
Dear experts,
I am using the Conn toolbox (https://web.conn-toolbox.org/), a Matlab package with a strong GUI funcitons. Curently I am doing repetitive stuff which I believe I can finish more easily with some scripting. However, it seems that there are no direct functions that I can call in Matlab and the work has to be done through clicking buttons and typing inputs into dialog boxes. I have traced down the code for the GUI and here is an excerpt from the function conn_displayroi:
data=get(hfig,'userdata');
name=data.names2reduced;
%name=get(state.handles.sphplots_txt,'string');
ok=true;
thfig=dialog('units','norm','position',[.3,.4,.6,.4],'windowstyle','normal','name','ROI labels','color','w','resize','on');
uicontrol(thfig,'style','text','units','norm','position',[.1,.85,.8,.10],'string',sprintf('New ROI label names (%d)',numel(name)),'backgroundcolor','w','fontsize',9+CONN_gui.font_offset,'fontweight','bold');
ht1=uicontrol(thfig,'style','edit','units','norm','position',[.1,.30,.8,.55],'max',2,'string',name,'fontsize',8+CONN_gui.font_offset,'horizontalalignment','left','tooltipstring','manually edit the ROI labels');
ht2=uicontrol(thfig,'style','edit','units','norm','position',[.1,.20,.8,.1],'max',1,'string','','fontsize',8+CONN_gui.font_offset,'horizontalalignment','left','tooltipstring','enter Matlab command for fast-editing all ROIs simultaneously (str is input variable cell array; ouput is cell array; e.g. "lower(str)")','callback','ht1=get(gcbo,''userdata''); set(ht1,''string'',feval(inline(get(gcbo,''string''),''str''),get(ht1,''string'')))','userdata',ht1);
uicontrol(thfig,'style','pushbutton','string','Apply','units','norm','position',[.1,.01,.38,.10],'callback','uiresume','fontsize',8+CONN_gui.font_offset);
uicontrol(thfig,'style','pushbutton','string','Cancel','units','norm','position',[.51,.01,.38,.10],'callback','delete(gcbf)','fontsize',8+CONN_gui.font_offset);
while ok
uiwait(thfig);
ok=ishandle(thfig);
if ok,
newname=get(ht1,'string');
if numel(newname)~=numel(name), conn_msgbox(sprintf('Number of labels entered (%d) does not match expected value (%d)',numel(newname),numel(name)),'',2);
else
delete(thfig);
data.names2reduced=newname;
ok=false;
end
else return;
end
end
So the aim of this piece of code is to change the 'data.names2reduced' property of the figure handle 'hfig' into 'newname'. It will open a dialog box 'thfig' with a text box 'ht1' that I have to fill in and a button 'ht2' that I have to click after I finish. Upon clicking the callback uiresume() will release uiwait(thfig) and the code continues to run. What I want to do is to write a script that helps me fill in the text box and click the button.
I am aware of the following answer to a similar situation (https://www.mathworks.com/matlabcentral/answers/95311-how-can-i-programmatically-invoke-the-callbacks-of-my-gui-in-order-to-automate-data-input-and-button). However, in this example, mygui.m is run first (i.e. not embedded in a function), and myscript.m is run later. There is no uiwait which stops execution. To reproduce the situation, I have included a simpler version (but I am not sure if this imitation of the code above is accurate or not):
testgui;
% what can I fill in here to let the script continue without button click?
disp('ok');
function testgui
a = figure;
uicontrol(a,'style','pushbutton','string','resume','callback','uiresume');
uiwait(a);
end
May I ask for your help to tackle this issuse? Thanks a lot for your help in advance.
Ralph
1 commentaire
Rik
le 10 Déc 2021
I don't actually understand what you want to do, but with your simplified example the answer is easy: you can put any code there, because the execution has stopped inside the testgui function. Since that statement has not returned yet, it doesn't matter what you put on the next line.
Réponses (1)
Voss
le 10 Jan 2024
0 votes
A Java Robot can be useful to automate GUI interactions.
Catégories
En savoir plus sur App Building dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!