Introduction

Matlab Logo 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:

inputdlg dialog box

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:

uigetfile dialog box

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.

uigetfile results

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:

uigetdir dialog box

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

uigetdir result

Other useful functions

Two other useful functions are fullfile and fileparts.

  1. The fullfile command 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!

    fullfile

  2. The fileparts command 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);

    fullfile

  3. This is the end of the tutorial.