Matlab - Querying the User For Input
11 Feb 2008 Quan Quach 27 comments 3,437 views
Introduction
In this tutorial, I will be discussing a couple of useful built in commands within Matlab that will allow you to write scripts/functions that are user friendly and flexible. These functions are input, inputdlg, uigetfile, and uigetdir. In the next tutorial, I will show you how to incorporate these commands into a GUI environment, so stay tuned.
Lets take a quick look at how these built in commands work.
The “input” command
The input command allows you to query the user for an input parameter. This can be extremely helpful for scripts or functions that require some user input. For example, lets use the input command to query the user for two numerical inputs. After that, we will add these two numbers together and display the result.
At the command prompt, copy and paste the following code:
userInput1 = input('Enter in the first number: '); userInput2 = input('Enter in the second number: '); total = userInput1 + userInput2; disp(['The sum of the two inputs is ' num2str(total)])
The input command is not just limited to numbers. You can ask for a string parameter or any other data type as well.
The “inputdlg” command
This command creates a dialog box that asks for user input, and does the functional equivalent to the code in the previous example. It looks a little nicer than the input command, but also a little more complicated. I would probably stick to the input command for scripts and functions, and use the inputdlg command for GUIs.
%notice this is a cell array! prompt={'Enter in the first number','Enter in the second number'}; %name of the dialog box name='Get user Input'; %number of lines visible for your input numlines=1; %the default answer defaultanswer={'0','0'}; %creates the dialog box. the user input is stored into a cell array answer=inputdlg(prompt,name,numlines,defaultanswer); %notice we use {} to extract the data from the cell array total = str2num(answer{1})+ str2num(answer{2}); disp(['The sum of the two inputs is ' num2str(total)])
Here’s what you should see when you run the code:

The “uigetfile” command
The uigetfile command is a very versatile one and offers many options. In this tutorial, we’ll just cover the basics of this command though. When this command is activated, a user dialog box pops up and allows the user to select a file. Once the user selects a file, the path name and file name are stored in their respective variables. Lets do a quick example.
Type in the following command at the matlab prompt:
[filename, pathname] = uigetfile('*.*', 'Pick any file')
Once you enter in the command, you should see the following dialog window appear:

Once you select any file, you’ll see that the filename and pathname have been stored accordingly! It’s not difficult to see why this command could be so useful.

There are an abundance of features that this command offers, so I suggest you use the matlab help to figure out all the nuances of uigetfile. For example, you can specify which file types that the user can select from, you can customize the dialog message, and you can even enable multi select, which allows the user to select more than one file at a time!
The “uigetdir” command
Similar to uigetfile, uigetdir instead allows the user to specify which directory they want to select.
Try typing this into the command prompt:
directoryName = uigetdir
You should see the following dialog box:

Choose any directory you please. Once you select your directory, the path will be stored in the variable directoryName:

Other useful functions
Two other useful functions are fullfile and fileparts.
-
The
fullfilecommand can prevent you from some serious headaches. Here’s a quick example of what it does:pathname = 'c:\myFolder'; filename = 'blinkdagger.m'; %one way of appending the data is: badWay = [pathname filename] %the best way to do it is using fullfile goodWay = fullfile(pathname,filename)
The fullfile command makes sure to use the appropriate backslashes so that the pathname is always well constructed. Notice that badWay is missing a backlash!

- The
filepartscommand is another command to keep in mind. This command will break up a file path into three parts: the directory, filename, and file extension. For example:myFile = 'c:\myFolder\blinkdagger.m'; [pathName,fileName,ext] = fileparts(myFile);

This is the end of the tutorial.
27 Responses to “Matlab - Querying the User For Input”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Oh my god, I’m about to start a pretty monumental program soon for another project and just reading through this casually has given me so many ideas on how to tackle the problems that have crossed my mind!
Yes!
Glad to be of help.
Great , This tutorial is wonderful
very nice, just what I needed, when i needed it. Just found the site, really excited
hm
Hello Freind,
I want to read different text file through GUI and want to match it
similarities .I have X Y and Angle data ,I used uigetfile command to
access the text file ,but it not read X Y and Angle data.
Is any other command to access files through GUI.
Damodar,
Take a look at the why xlsread works by typing “help xlsread” at the command prompt.
I think you might want to do something like :
Hello,
Thanks for reply,
I have used xlsread,to read values ,but it reads only predefined files,I want to read files on user interest,mean user can select different (.txt ot .xls)files through GUI .
Damodar,
The uigetfile command is very versatile and allows you to specify which file types you want to open. If you look at the help for uigetfile, it will show you how to specify which file types you want to open.
And if you’re looking to parse the data within a .txt file, you should check out the following link for some parsing help:
http://www.blinkdagger.com/matlab/parse-data
Hello Quan,
I have used uigetfile to read two text file test1.txt and test.txt, it read file but I am not able to acess (X Y Angle,X1 Y1 Angle1 ) values.
test1.txt
X1 Y1 Angle1
95 158 2.62
84 160 2.62
168 163 2.62
111 171 2.62
test.txt
X Y Angle
102 158 2.62
86 160 2.62
177 163 2.62
116 171 2.62
Damadar,
see the link i provided in the previous comment.
Hello Quan
From the code above,how can I simply display the filenames only in the listbox?
Thanks
Hello Quan,
i used uigetfile to read text files test1,test2,test3 into listbox. I tried to plot one at a time. Only deleting two other files i am able to plot one. I need help.
Wer nice documan thanks
Thanks for all the information you have provided..it will be a great help 2 use these…
good tutorial
I got some useful information . Thanks
I also would like to know..how can we open a popup window having combo box( to select the input from number of choices listed in the box) input using Matlab coding. Is it possible to use combo box as input in matlab?
I will appreciate if anybody could help me out through this.
thank you
Deepak
what is the optimum way to ask a user to select their data file.. then load the file they selected.. Thanks
is there a way how to output to a msg box:
Example:
you get a msg box saying:
The error obtained is: 3m
where 3m will be a variable obatined from the mfile.
thanks
Thanks, very useful! Short and sweet.
This’s an awsome tutorial, great job. thanks
Anon,
Try something like this for error dialogues using a variable:
The key is to make sure each input to errordlg is a ’string’ class and if you want to have your variable and extra text, toss them in a vector using [] shown above.
type:
doc errordlg
in the command window for more detailed help.
-Zane
Hey whn clicked on cancel, it gives me error “Index exceeds matrix dimensions” as it returns empty array. But how can i put an “IF condition” that if array is empty it should return to the same figure on the window.. Thnx in advance..
Man…I love you…lol
Just what I had in mind but never know what the function names were…Anyways, another one for your collection…”find_system” might be useful for those who are looking a way of finding specific block in simulink file… cheers
how to get an input signal from a user
Hi
your tips are great and very useful but im having a problem with the above code.
say if i enter A = 0.0 in my dialog box, the if part of the code should make all the other values equal 0.0 but they dont, they stay the value that they are entered in the dialog box.
Any ideas?
thank you
I fixed it it was suppose to be str2num not num2str
This is amazing, I can imagine how much time i would have spent looking for these things. You are a life saver. BIG-UPS
Thank you