Matthew Kemnetz
Matthew Kemnetz

Reputation: 855

Simple pushButton with changing text in MATLAB

I am trying to implement a very simple GUI that consists of just one pushButton. I want it to begin by just having START as a label. Then on press it changes to STOP. When the user clicks the button the first time the callback sets a boolean to true and changes the label. When the Button is clicked a second time the boolean is changed to false and the GUI closes.

I can't find anything on how to make a simple GUI like this in MATLAB. the GUIDE tool makes no sense to me and seems to generate so much useless code. Matlab buttons are wrappers for jButtons as seen here

Upvotes: 6

Views: 4099

Answers (1)

Jonas
Jonas

Reputation: 74940

GUIDE is quite straightforward - the automated tool generates stubs for all the callbacks, so that all is left is to fill in the code to be executed whenever the callback runs. If you prefer to create the GUI programmatically, you can create the button you want as follows:

%# create GUI figure - could set plenty of options here, of course
guiFig = figure;

%# create callback that stores the state in UserData, and picks from
%# one of two choices
choices = {'start','stop'};
cbFunc = @(hObject,eventdata)set(hObject,'UserData',~get(hObject,'UserData'),...
          'string',choices{1+get(hObject,'UserData')});

%# create the button
uicontrol('parent',guiFig,'style','pushbutton',...
          'string','start','callback',cbFunc,'UserData',true,...
          'units','normalized','position',[0.4 0.4 0.2 0.2])

Upvotes: 4

Related Questions