# The GUI way of doing things16

Posted by Michael Katz,

While I like to write software for my own pleasure, sometimes I write programs to be used by other people--meaning I have to implement a user interface. I like the input function for asking simple questions of the user. Its syntax is straightforward, but its output clutters the Command Window, allows the user to make mistakes, is annoying for typing long answers (like a file name and location), and most importantly it makes the program feel like the interactive fiction I used to write in BASIC. Fortunately MATLAB comes with a lot of functions that build simple GUIs for asking the user questions.

The most basic of these is the inputdlg function. Instead of just input:

a=input('What kind of Peanut Butter would you like? ','s')

you could use

b=inputdlg('What kind of Peanut Butter would you like?')

Of course, this leaves the user with the availability of typing anything! He could say that he wants oranges, which is not a valid option (or at least not at my local Stop & Shop). Unfortunately MATLAB provides dialogs with ways of restricting the user's choices. Each one with its pros and cons.

The listdlg provides the user with a list of options. It's by far the ugliest, and you can see by default the sizing is bad and it crops our question.

c = listdlg('PromptString','What kind of Peanut Butter would you like?',...
'SelectionMode','single', 'ListString',{'Crunchy','Creamy','Natural','Chocolate'})

Fortunately, there is an easy fix with some additional inputs to listdlg:

c = listdlg('PromptString','What kind of Peanut Butter would you like?',...
'SelectionMode','single', 'ListString',{'Crunchy','Creamy','Natural','Chocolate'},...
'Name','Select Peanut Butter','ListSize',[230 130])

If you have three or less options, you can go the questdlg route:

d = questdlg('What kind of Peanut Butter would you like?', 'Peanut Butter Selection', ...
'Crunchy','Creamy','Natural','Creamy');

Finally, you could also use a menu dialog to present one button per choice:

e = menu('What kind of Peanut Butter would you like?',...
'Crunchy','Creamy','Natural','Chocolate')

As you can see none of these are as pretty as one could make a custom dialog, however they are quite easy to code and useful for programming user interactions. Even more compelling than these general choice dialogs are the ones provided that allow the user to select a file, color, or font. Those will be the topic of another post. You can find more about them here.

## 16 CommentsOldest to Newest

Adam Attarian replied on : 1 of 16

Really, my only comment from this post is…they have chocolate peanut butter?! I learn so much from these blogs!

Ken replied on : 2 of 16

@Adam,

I said the same thing to Mike — “they have chocolate peanut butter?!”. It sure does sound delicious.

-Ken

Petter replied on : 3 of 16

The fact that the first listdlg attempt fail is embarrassing! Also, MATLAB really needs to freshen up its GUI dialogs. The last example looks like somthing dug up from Windows 95.

Petter replied on : 4 of 16

This is one reason MATLAB is slowly losing customers to SciPy/NumPy, I think.

Matt Fig replied on : 5 of 16

Petter,

Where do you get your information that MATLAB is slowly losing customers to SciPy/NumPy? I hope you can point to more than some newsgroup banter, or a few personal experiences. Just curious.

About the example GUI appearance: This looks like xp more than windows 98 to me. On my Vista machine the GUI looks very modern.

Petter replied on : 6 of 16

Sorry for not clarifying me more.

The trend that SciPy is growing strongly is that many of my coworkers (in academia) are trying to use MATLAB less and less. They are writing books on Python and encouraging others to “switch”. The reasons stated are among others the many libraries available for GUI design in Python.

This is indeed my personal experience in academia, but this is definitely a trend among the people I meet. So far, I have continued to use MATLAB myself.

Well, the title bar in the last example certainly looks XP, but the color palette of the actual contents looks more 90’s-ish.

Mike replied on : 7 of 16

That’s because the menu function is ten years old. It’s still hanging around for backwards compatibility. The state of the art in UI design is not to have long lists of buttons so I hope it’s not actually still used much.

wei replied on : 8 of 16

These are nice small examples of matlab GUI. Over the years handle graphics (HG) has made many advances. But a major refreshing of handle graphics is highly desireable and will keep it updated for GUI development.

There are two reasons for this: user expectation is higher and other development environment is easier. One evidence is that the Mathworks’ own GUI for various toolboxes are moved away from HG more and more, for whatever the reason.

Scott Hirsch (@TMW) said once that HG will get much better. I hope this promise is realized soon.

Scott Hirsch replied on : 9 of 16

It’s great as always to hear the different perspectives from readers of this blog (who knew about chocolate peanut butter??). As for the future of graphics and GUI building in MATLAB – we recognize these as core capabilities of the MATLAB platform and are very committed to evolve these features to leverage evolution in technology since they were originally released. We have nothing to announce right now, other than that we’ve been listening and are working really hard for you!

Amy replied on : 10 of 16

What about a function called popupdlg that would display instead of a list box and popup menu?

Amy replied on : 11 of 16

That should be “instead of a list box, a popup menu”

Mike replied on : 12 of 16

Amy,

I will bring your suggestion to the developers. Thanks!

us replied on : 13 of 16

…now i’m really hungry…
us

Stephen L. Morgan replied on : 14 of 16

Why can’t dialog boxes in Matlab support a default button assignment so that the user does not have to use the mouse when the focus is on the button desired?

Yair Altman replied on : 15 of 16

@Stephan – Matlab does support default dialog button – see the example of ‘creamy’ in the questdlg snippet in Mike’s original post above.

Jette replied on : 16 of 16

These predefined dialog boxes are unfortunatly missing support for multiple monitors (at least in R2010b). We have more and more computers with several monitors and it is very annoying when dialogs appear on a different monitor than the GUI that calls the dialogs.