Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 772
Matthew Kemnetz Avatar asked Jan 03 '13 23:01

Matthew Kemnetz


People also ask

How do you start a new line of text in Matlab?

c = newline creates a newline character. newline is equivalent to char(10) or sprintf('\n') . Use newline to concatenate a newline character onto a character vector or a string, or to split text on newline characters.

How do I write text in Matlab?

text( x , y , txt ) adds a text description to one or more data points in the current axes using the text specified by txt . To add text to one point, specify x and y as scalars. To add text to multiple points, specify x and y as vectors with equal length. text( x , y , z , txt ) positions the text in 3-D coordinates.

How do I create a push button in Matlab?

btn = uibutton creates a push button in a new figure and returns the Button object. MATLAB® calls the uifigure function to create the figure. btn = uibutton( style ) creates a button of the specified style. btn = uibutton( parent ) creates the button in the specified parent container.


1 Answers

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])
like image 85
Jonas Avatar answered Sep 20 '22 03:09

Jonas