Introduction to Computing
using Matlab
ENGR 113
Using Script Files and Managing Data – B
Chapter 4
1
Using Script Files and Managing Data
In these series of lectures will learn
• How to input data into a script file
• How MATLAB stores data
• Ways to display and save data
• How to exchange data between
MATLAB and other programs
2
Importing and Exporting Data
• MATLAB often used to analyze data collected by other programs
• Sometimes need to transfer MATLAB data to other programs
• In this section will only discuss numerical data
• MATLAB has commands to load and save data from a number of other programs
• Can also tell MATLAB what format data is in
• Make sure that there are no empty columns or rows in the middle of
populated columns or rows.
3
Importing and Exporting Data
Will illustrate transferring data with a specific program by
discussing Microsoft Excel
• Commonly used to store data
• Works with many programs that gathers data
• Used often by people with technical data but for which
MATLAB is overkill
4
Importing and Exporting Data
Importing and exporting data into and from Excel:
Import (read) data from Excel with
variable_name=xlsread('filename')
• Stores all data in one variable
• If Excel file has multiple sheets, reads first one
• To read from other sheets, pass command the sheet name
variable_name = xlsread(‘filename’, ‘sheet_name’)
• Can read rectangular section of sheet by specifying range in
command
variable_name = xlsread(‘filename’, ‘sheet_name’, ‘range’)
• Range example: ‘C2:E5’
5
Example 1
h=xlsread('test')
h=
1 8 100059157
2 8 100059479
3 8 100059819
4 8 100059958
5 8 100060674
6 8 100060704
7 8 100060805
8 8 100060809
9 8 100060882
• If no sheet name, the first sheet is selected
• If no range, the entire sheet is selected
• The first row is empty, so it was ignored
• All empty side rows and columns were ignored
6
Example 2
Empty set, first row is empty
Select one row
Select multiple rows
Empty columns are ignored
7
Example 3
8
Export (write) data
Export (write) data to Excel file with
xlswrite('filename',variable_name)
• ‘filename’ (typed as a string) is the name of the Excel file to which the
data is exported.
• The file must be in the current directory. If the file does not exist, a new
Excel file with the specified name will be created.
• variable_name is the name of the variable in MATLAB with the assigned
data that is being exported.
• The arguments ‘sheet_name’ and ‘range’ can be added to the xlswrite
command to export
9
Example 1
>> x=1:2:10;
>> xlswrite('test1',x)
>> y=randi(5,5);
>> xlswrite('test1',y)
>> xlswrite('test1',y,'A1:C1')
10
Example 2
>> xlswrite('test1',y,'SS2','A1:C1')
>> xlswrite('test1',y,'SS2','A1:A3')
11
Using the Import Wizard
MATLAB's import wizard is semi-automatic way to read data
from any file
• Wizard shows what it thinks format is
• User can then adjust format
Two ways to start Import Wizard
1. In MATLAB desktop, click Import Data icon
2. With command uiimport
12
Using the Import Wizard
First Wizard display
• Wizard displays file-selection dialog box
• User picks file
• Wizard shows some of data as it is in file and as how Wizard
interprets it
• User can change column separator or number of text header lines
(that Wizard will not try to read)
13
Using the Import Wizard
Second Wizard display
• Shows name and size of variable it will create
• When user selects Finish, Wizard creates that variable in workspace
• Variable name is file name
14
Questions?
15