Matlab GUI - Using “msgbox” to Create a Better GUI
18 Feb 2008 Quan Quach 18 comments 4,781 views
Introduction
The msgbox command in Matlab can provide valuable information to the user of your GUI. It can warn the user, inform the user of an error, and also provide help. In this example, we will use a GUI (shown below) consisting of a simple edit text component.

The GUI takes in inputs between 0 to 100.
-
If the user tries to input a numerical value that is not within 0-100, then the GUI will spit out an error message.

-
If you user tries to set the value to 100, the user will give off a warning message.

-
If you try to input something that is not a number, the user will get a help message.

The Example
-
First, download the GUI skeleton here. Unzip the files and place them wherever you please.
-
Now, type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
Here is what the GUI should look like when you open it:

-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
Now, we want to add the following code to edit1_Callback.
input = get(handles.edit1,'String'); %get the input from the edit text field input = str2num(input); %change from string to number if isempty(input) %if the input is not a number %this is the first line of the msgbox msgboxText{1} = 'You have tried to input something that is NOT a number.'; %this is the second line msgboxText{2} = 'Try an input between 0 and 100 instead.'; %notice that msgboxText is a Cell array! %this command creates the actual message box msgbox(msgboxText,'Input not a number', 'help'); elseif input == 100 %if the input is exactly 100 msgboxText{1} = 'You have chosen the highest possible input!'; msgboxText{2} = 'You cannot go any higher than this or it will result in an error.'; msgbox(msgboxText,'Maximum input chosen', 'warn'); elseif input < 0 || input > 100 %if the input is less than 0 or greater than 100 msgboxText{1} = 'You have chosen a value outside the range of 0-100!'; msgbox(msgboxText,'Input not allowed', 'error'); end
-
And that’s it! Run the GUI to make sure that it does what you expect it too.
Other tips
The msgbox command can be replaced with the errordlg, warndlg, and helpdlg commands. It’s purely a matter of preference. For example:
msgbox('hello world', 'hello world','error'); %an equivalent command errordlg('hello world','hello world')
Download the Source Files
You can download the source files here.
18 Responses to “Matlab GUI - Using “msgbox” to Create a Better GUI”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Dear Quan,
All ur tutorials have proved to be very useful to me.
I’ve a doubt regarding MATLAB GUI figure window resizing.
Can u please explain in detail how to do it?Its really urgent as am now stuck with my project as i cant findout how to do it.
Thanks and regards,
Sharina
Hey Sharina, I’ve been browsing through some of the Blogs @ Mathworks and either Doug or Steve (probably Doug actually) has a short video on window resizing. You could try that out if your project isn’t already past its deadline. Quan may have a different approach too though, so you’d probably do yourself good checking back here often too.
Thanks a lot Qi-Ian Poon…
Another doubt is :
Can we add ‘Yes’, ‘No’ buttons to msgbox?
if yes, how?
hoping to get a reply soon..
Sharina,
Yes you can! That’s actually the next tutorial I have planned. Stay tuned! It involves the use of the command questdlg.
Is it possible to create a loading dialog box, that does not have an option to press OK, but rather is displayed while a calculation takes place and closes when that calculation is done?
Thanks
Hi Rod,
Yes, it’s possible to do this. When faced with the problem you described, I just disable all of the buttons and change the mouse cursor so taht the use knows that the GUI is processing data.
http://www.blinkdagger.com/matlab/matlab-gui-tutorial-disable-mouse-clicking
Try that tutorial!
thanks a lot…i was a great help
is there a way to use the message box to return values or strings?
like fprintf does. i.e.
statement1=’the number is’
number1=3.14
fprintf(’%s %1.2f’, statement1, number1)
output>> the number is 3.14
likewise can you make the message in msgbox return values/strings like these?
Kaspi,
Try this tutorial:
http://www.blinkdagger.com/matlab/matlab-querying-the-user-for-input
How to keep prompt box in my GUI,so that i can enter a value in it at run time?
Thanks a lot.Quan Quach Sharina.A tutorial on MATLAB-Querying-the-user-for-input is very useful one.I tried those examples.
hi,
i have a doubt, how to input array matrix in matlab graphical user interface.
please reply soon
hi
i hv a doubt how to input an array matrix in graphical user interface.
When I call msgbox after plotting a figure, all the data plotted in the figure gets reset to zero. Why?
I have created an edit box where i want to display the output error after some calculations.
htext71 = uicontrol(’Style’,'edit’,'String’,”,’position’,[850 480 50 15]);
what code or function do i need to output the error.
thanks
H! Quan, Thank you and this does not work ,when I try :
a=1;
msgboxText{1} = ‘Hi’;
msgboxText{2} = a;
msgbox(msgboxText)
Error:
Input must be a string.
any help?
Bashaer, simple fix. Whenever you want to display something (including numbers), it needs to be in a string format. Just like ‘Hi’ is a string, you need to convert a=1 into a string as well. See the change below.
-Zane
Dear friends,
does anyone know how to make “edit text” arrays in matlab?
for example, suppose that I have 20 “edit text” s in my gui, and I’m about to send their texts to 20 cells of a matrix via a “for” loop. is it possible to treat the edit texts as members of an array, working with its index?
you know, in many object oriented programming languages such as visual basic , this is possible by defining the text boxes as members of an array, using the “for” loop counter as the index variable for the mentioned array.
but I have no Idea abaut this in matlab.
I would be so thankful if anyone could help me in this case, or in finding any other way to transfere the text of a large number of “edit text” boxes to a matrix.
please inform me at : radi_count_001@yahoo.com
thanks.