File Exchange

image thumbnail

uibutton: GUI pushbuttons with better labels

version (3.13 KB) by Douglas Schwarz
Creates pushbutton uicontrol labeled with TeX or LaTeX strings.


Updated 05 Nov 2010

View Version History

View License

Have you ever wanted to create a pushbutton uicontrol containing a Greek letter, some other mathematical expression or even just a multi-line label? This function will do it for you. It is very easy to use as it accepts all the same arguments as uicontrol with the addition of the Interpreter property which you can set to 'tex' or 'latex' and the Rotation property to set the angle of rotation of the text. Also, you can pass in a cell array of strings for the String property to get a multi-line label. For example,

uibutton('String','Set \beta','Interpreter','tex')

Can also create something that looks like a text uicontrol.

Cite As

Douglas Schwarz (2021). uibutton: GUI pushbuttons with better labels (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)


Thank you for the script. However it dont work properlly as mentioned bellow. We we have a maximized figure it change its size as well as the button size the buttons become too small and we cant see the string. Moreover it apear a line in the midle of a vertical button (the line is also vertical). Note that I was using also panels.

Iman Alsharkawi

So this works pretty well but has issues with handle visibility. I am building stacked panels in an interface and uibutton does not function properly when I create objects of this type on stacked panels.

For example, on the visible panel, I can create uibuttons and see them just fine. But when I switch to make another panel visible, the buttons on the new panel show up as outlines, but no text or correct properties of uibutton. Switching back to the first visible panel causes no issue to the original buttons on.



Gregory Vernon

Bravo. Does what it says it will do. There is a warning I receive in 2011b however:

Warning: The specified rectangle is not fully
contained within the figure. This will become an
error in a future release of MATLAB.
> In getframe at 68
In uibutton at 164

Douglas Schwarz

Oliver, thanks for the comments. I was able to reproduce this behavior. I would argue that it's the result of a bug in getframe -- getframe ought to work since the figure is on screen, just not the main screen. Anyway, I have some ideas on how to work around it by temporarily moving the figure to the main screen and back again so watch for an update.



If the GUI which utilized the function is on a second display, the following error occurs:

?? Error using ==> capturescreen
The rectangle passed to getframe must be at least partially on screen

Error in ==> getframe at 105
x=builtin('capturescreen', varargin{:});

Error in ==> uibutton at 155
frame = getframe(ax);

Error in ==> hilDataRecorder>hilRecorderPreviewBtn_Callback at 125


In my case, I'm changing the string on a togglebutton object and would like the button to have two lines of text. So if the GUI is ever run on a second display, the function fails.

Great tool, nonetheless!



Matthew Adler

When I try to run this I get an error. I am able to generate the pushbutton with greek symbol, to size it and place it in the position I want with code:

alpha_button = uibutton('String','\alpha','Interpreter','tex','Position', [219 570 50 25]);

When I execute my GUI, however, the callback from my main pushbutton is not accessed. Also, if I move the symbol pushbutton into any position with 'position' I get the following error:

Error in ==> getframe at 35

Error in ==> uibutton at 146
frame = getframe(ax);

Error in ==> lcgui>lcgui_OutputFcn at 78
alpha_rand = uibutton('String','\alpha','Interpreter','tex','Position', [219 570 50 25]);

Error in ==> gui_mainfcn at 248
[varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);

Error in ==> lcgui at 40
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

Error in ==> Lehigh_Corrosion_Fatigue at 10

??? Error using ==> Lehigh_Corrosion_Fatigue
Error using ==> capturescreen
The rectangle passed to getframe must be at least partially on screen.

??? Error while evaluating uicontrol Callback

Micke M

I would like to know where in the GUI-code to put the uibutton(...);

(For those of us that are quite new to GUIs)

ding lu

very good

Agnieszka Herman

Great! Works without any problems and it saved me really lots of time. Thank you!

Trevor Fichera

Just what I needed. I am using it in Matlab Ver. 6.5.0 (R13.0.1). I had trouble with the isscalar(h) command, but I replaced it with ~any(size(h)-1) and it works great.
For multiline text buttons, try
uibutton(h,'String',' Print\newlineReport','HorizontalAlignment','center')
where h is the handle of an existing uicontrol button.

If anyone knows of a good LaTeX reference online, I would appreciate the post.

ebrahim sepidbar


Jiro Doke

Very nice. I like how you create an image and use that as an ICON for the buttons. This guarantees that it behaves exactly like uicontrols.

One comment: when the style is TEXT, it creates a text object. In the code, you comment that it doesn't return the handle because it is not a UICONTROL. You should probably say something about that in the help text. Also, if someone (by mistake) asks for the handles, as in h=uibutton('style', 'text', ...), it gives an error because hout is not assigned. Maybe there's a better response to this, such as returning an empty variable or displaying a message.

Peter Nave

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!