CS 112

 MATLAB Extras

This document describes some additional MATLAB examples that may be useful for your final projects, illustrating the following topics:

Selecting a File for Reading or Writing with uigetfile and uiputfile

Textboxes with Multiple Lines

Recording Mouse Clicks

Tips on Working with Excel Spreadsheets

The GUI code and other files for these examples can be found in the MATLAB_extras folder in the CS112 download directory. There is a single GUI based program named extrasGUI that can be executed to run these examples. The MATLAB Help facility provides further documentation about each of the functions described below.

Selecting a File for Reading or Writing with uigetfile and uiputfile

The built-in MATLAB functions uigetfile and uiputfile allow the user to navigate to a desired folder and select a particular file for reading, or to specify a desired file name and file type for writing. When the extrasGUI program is first called, the following GUI is displayed:

If the get image file button is pushed, the uigetfile function is called and a dialog box appears, allowing the user to navigate to a desired folder and select an image file to be loaded:

By default, you may find that only MATLAB files are enabled, but if you select All Files in the menu labeled Enable: near the bottom of the window, all files will be enabled. In the example provided in the extrasGUI.m code file, all files are enabled because the first input to uigetfile indicates a general format for the file name:
imfile = uigetfile('*.*', 'Select an image file');
There's an image file named Wellesley.png in the MATLAB_extras folder that contains a campus map that can be selected here. The image is read in using the imread function and displayed on the GUI:

If the save text button at the top of the GUI window is pressed, the uiputfile function is called and a dialog box appears that allows the user to navigate to a desired folder and specify a file name and type of file to write. You can select a name for a textfile (such as cs110.txt in the example below), select All Files in the menu labeled File Format: and click on the Save button. The code then saves the current course description (displayed on the GUI) in the new text file.

The following code performs these actions:
% allow user to navigate to desired folder and specify file name for saving
fileName = uiputfile;    
fid = fopen(fileName, 'w');    % open file for writing
% write the current course description into the text file
courseDescription = get(handles.courseText, 'String');
for i = 1:length(courseDescription)
    fprintf(fid, '%s\n', courseDescription{i});
end
fclose(fid);    % close the text file

Textboxes with Multiple Lines

The example illustrated above contains a Edit Text box in which a course description is displayed with multiple lines of text. Selecting a different introductory CS course from the pop-up menu results in a different description being displayed in the textbox. The text for these descriptions is read from a textfile named courseInfo.txt that has the following content:

***
Computer Science and the Internet

Students build Internet applications
using HTML, CSS and JavaScript
***
Programming and Problem Solving

In-depth introduction to computational
thinking and programming using Java
***
Computation for the Sciences

In-depth introduction to computational
thinking and programming using MATLAB
***
The Socio-Technological Web

Introduction to aspects of Computer 
Science that impact society
***
Inventing Mobile Apps

Intro to Computer Science through
creation of apps for mobile devices

In the opening function in the extrasGUI.m code file, the contents of the courseInfo.txt file is read in and stored in the handles structure in a variable named handles.courseInfo that is a cell array containing separate strings corresponding to each line of the above text file. The following code implements this:

% load information about CS courses from text file
fid = fopen('courseInfo.txt');
handles.courseInfo = textscan(fid, '%s', 'delimiter', '\n');
handles.courseInfo = handles.courseInfo{1};
fclose(fid);

The number of lines of text that can be displayed in an Edit Text or Static Text box is controlled by the Max property for the textbox and can be set in the Property Inspector window when creating the GUI layout using the guide program. In order to display multiple lines of text, the Max property should be set to an integer that is larger than 1.0. In the extrasGUI example, there is a textbox whose Tag name is courseText. The value of the Max property for this textbox is 4.0, as shown below:

Multiple lines of text can be placed in the textbox at the outset by using the icon provided for the String property. From a code file, the text can be displayed by setting the value of the String property to a cell array of strings corresponding to the multiple lines of text, for example, the following code statement:

set(handles.courseText, 'String', {'one' 'two' 'three'});

results in the following display of content in the textbox:

In the extrasGUI.m code file, the course description for the selected course is displayed in the textbox with the following code:

index = get(hObject, 'Value');       % get index of selected course
% get indices of the starting lines for all courses
startIndices = 1 + find(strcmp(handles.courseInfo, '***'));
% load desired course description into the textbox
start = startIndices(index);
set(handles.courseText, 'String', handles.courseInfo(start:start+3));

As described earlier, handles.courseInfo is a cell array containing the strings of text read from the courseInfo.txt textfile. In the last statement in the above code, the String property for the textbox is set to the value handles.courseInfo(start:start+3). Parentheses are used here to specify a range of indices in order to create a cell array that contains the strings stored in the handles.courseInfo cell array from the index start to the index start+3.

Note that if you only need to display a small number of lines of text in the textbox that will always be accommodated by the size of the textbox that is set with guide, then either a Static Text or Edit Text box can be used. However, if you want to display many lines of text and want the user to be able to scroll through the text, then an Edit Text box must be used.

Recording Mouse Clicks

The getpts function can be used to record the positions of a series of mouse clicks that are made in a figure window or axis area. In the extrasGUI example, if the user clicks on the get mouse clicks button, the getpts function is called. The user can then click on a series of locations on the campus map and the x,y coordinates of each mouse click will be recorded and stored in vectors. The final location should be specified with a double-click. The plot function is then called to draw a plot of the sequence of mouse positions superimposed on the map, as shown below:

The following code implements these actions:

[xcoords ycoords] = getpts(handles.imageAxes);
% create a plot on the image that connects the user's mouse positions
axes(handles.imageAxes);
hold on
plot(xcoords, ycoords, 'r', 'linewidth', 2);

Tips on Working with Excel Spreadsheets

There is a MATLAB Toolbox that is designed for reading and writing general Excel spreadsheets, but unfortunately this Toolbox is not provided with the version of MATLAB available on public computers at Wellesley. The basic version of the xlsread that comes with the core MATLAB system does not appear to work correctly on spreadsheets created with the current version of Excel that creates files with a .xlsx file extension. The following steps can be used to transform such spreadsheets into a form that can be read with the basic xlsread function.

The following general spreadsheet was downloaded from a U.S. government website:

A section of this original spreadsheet was then copy/pasted into a new Excel Workbook:

The format of the numbers, which contain commas, will be problematic for reading into MATLAB, but this format can be changed. To do this, select the contents of the spreadsheet and then select the Cells... option from the Format menu at the top of the Excel window. The following dialog box appears (the Number category is selected on the left):

The box labeled "Use 1000 Separator (,)" should be unchecked and the proper format for negative numbers should be selected, as shown in the above picture. Clicking the OK button will apply this formatting, which results in the following content:

Finally, the new spreadsheet was saved using the Excel 97-2004 Workbook (.xls) format, as shown below:

This new spreadsheet, dataNew.xls, can then be read into MATLAB. This file, as well as the original excerpt contained in data.xlsx, can be found in the MATLAB_extras folder. In the opening function in the extrasGUI code file, the dataNew.xls spreadsheet is read into MATLAB using xlsread:

[numData txtData] = xlsread('dataNew.xls')

There is no semi-colon at the end of the statement, so the values of numData and txtData are printed in the Command window. numData is a matrix storing the numerical data from the spreadsheet and txtData is a cell array containing the strings of text stored in the first column of the spreadsheet. If three output variables are provided when calling the xlsread function, as shown in the following example, the third variable will be assigned to a 2-D cell array containing all of the data in the spreadsheet.
[numData txtData allData] = xlsread('dataNew.xls')

In this example, textual data from the spreadsheet will be stored as strings in the corresponding cells of allData and numerical data from the spreadsheet will be stored as numbers in the cells of allData.