Download presentation
Presentation is loading. Please wait.
1
Advanced Data Import & Export Jeff Henrikson (jhenriks@umd.edu)
Lecture 13 Advanced Data Import & Export Jeff Henrikson 1
2
Outline File I/O uiimport import/export Excel 2
Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 2
3
File I/O .txt .xls .mat .txt .xls .mat Read from Write to MATLAB
Problem areas: Mixing character strings and numbers Reading and writing formatted data a b 0.0 288.15 11.0 216.65 Show file extension: Finder -> Preferences... -> Advanced -> check box "Show all file extensions" Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
4
Import/Export ASCII File
ASCII File with Known Data Format: Import Export csvread dlmread textscan textread fscanf csvwrite dlmwrite Cell Array {..} – access cellplot – visualize celldisp – display File with Unknown Data Format: uiimport Import Wizard Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
5
Read URL data urlread reads in as an array of all single characters from the http url source file that contains both data and formatting text Work around data structure strfind – find one string within another length – get the length of a vector Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
6
Data (mht=>txt=>xml)
Convert mht to txt data File/Save as type: Text File Then delete any content not needed Or copy the needed content to Notepad Convert txt to xml data Microsoft Office Excel File/Open… .txt Delimited Next> Tab & Space Finish Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
7
Export to Excel We want to save the following table to an Excel file
General format: xlswrite (filename, variable, Sheet, Range) 216.65 11.0 288.15 0.0 b a Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 7
8
Excel Data Structure xlswrite (filename, variable, Sheet, Range) 8
Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 8
9
Excel Data Structure xlswrite (filename, variable, Sheet, Range)
Range = B1 Data will be written starting at cell B1, taking up as many rows and columns as required. Range = D3:G8 Data will be written in the box with corners specified by D3 and G8. Extra data will be discarded. Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 9
10
Export to Excel Example
Open new M-file and save as L13E1.m close; clear; clc; % Define 2 column vectors a and b a=[0.0; 11.0]; b=[288.15; ]; 216.65 11 288.15 b a Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 10
11
Export to Excel Example
% Write the header in the specified location xlswrite ('L13E1.xls', 'a', 'Sheet1', 'A1'); xlswrite ('L13E1.xls', 'b', 'Sheet1', 'B1'); % Write data in the specified location xlswrite ('L13E1.xls', a, 'Sheet1', 'A2'); xlswrite ('L13E1.xls', b, 'Sheet1', 'B2'); Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 11
12
View Example Excel Output
Current Directory Window Right click Open Outside MATLAB Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
13
Write headers to Excel In L13E1.m we wrote 1-character headers
Let’s try writing nicer headers 216.65 11 288.15 b a Height Temperature Level 1 288.15 Level 2 11 216.65 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 13
14
Write headers to Excel MATLAB will write each character in a separate cell! We have to write text strings as cells, not arrays Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 14
15
Excel header Example Open new M-file and save as L13E2.m
close; clear; clc; % Define 2 column vectors a and b a=[0.0; 11.0]; b=[288.15; ]; 216.65 11 288.15 b a Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 15
16
Create Excel header Cell
Create a cell array with all headers % First create a 3x3 empty cell array head = cell (3,3); % Then fill cells with row and column headers head{1,2} = 'Height'; head{1,3} = 'Temperature'; head{2,1} = 'Level 1'; head{3,1} = 'Level 2'; Height Temperature Level 1 Level 2 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
17
Excel header Example % Assign the Excel file name for output
filename = 'L13E2.xls'; % Write headers to Excel file xlswrite(filename, head, 'Sheet1', 'A1'); % Write data to Excel file xlswrite(filename, a, 'Sheet1', 'B2'); xlswrite(filename, b, 'Sheet1', 'C2'); Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
18
View Example Excel Output
Current Directory Window Right click Open Outside MATLAB Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
19
Import from Excel >>uiimport [Enter]
Browse and select file L13E2.xls Click Names to preview data Check boxes to select data Click Finish Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 19
20
uiimport Numeric variables are imported as array 'data'
Text strings are imported as cells 'textdata' If you check Generate M-code, MATLAB will create an M-File to read the data. Next time, you can just type: >>importfile ('L13E2.xls') [Enter] Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang 20
21
Import from Excel In Command Window >> whos [Enter]
Name Size Bytes Class Attributes data 2x double textdata 3x cell >> data [Enter] data = >> textdata [Enter] textdata = '' 'Height' 'Temperature' 'Level 1' '' '' 'Level 2' '' '' Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
22
Import from Excel uiimport works well if you have a single file
Multiple data files: read data from inside the M-file (like textscan) Command to use: xlsread Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
23
Import with xlsread >> mydata = xlsread ('L13E2.xls', 'Sheet1');
>> [mydata, header] = xlsread ('L13E2.xls', 'Sheet1') mydata = header = '' 'Height' 'Temperature' 'Level 1' '' '' 'Level 2' '' '' Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
24
Import Excel data file xlsread('filename.xls')
Before using this command, you need to ensure that where the file is stored is in your search path (File Set path) If Excel is not installed on your computer, MATLAB cannot use xlsread or xlswrite Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
25
xlswrite Exercise Write code L13E3.m:
Use xlswrite to create L13E3.xls for a table: Use cell arrays Sport Name \ Score Wins Loses Ties University Soccer 12 7 2 University Hockey 15 6 3 University Baseball 10 5 4 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
26
Cell Array Assignment clear; clc c = cell(3,3); c{1,2} = 'string'
'r1 c1' 'r1 c2' 'r1 c3' 'r2 c1' 'r2 c2' [] 'r3 c1' 'r3 c2' [] clear; clc c = cell(3,3); c{1,2} = 'string' c(1,2) = {'string'} % same as above c(1,:) = {'r1 c1', 'r1 c2', 'r1 c3'} c{1,:} = {'r1 c1', 'r1 c2', 'r1 c3'} % wrong c(2:3,1) = {'r2 c1', 'r3 c1'} c(2:3,1) = {'r2 c1'; 'r3 c1'} % same as above c(2:3,1:2) = {'r2 c1', 'r2 c2'; 'r3 c1','r3 c2'} Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
27
Exercise Code % close file and clear workspace close; clear; clc;
% Assign the output file name filename = 'L13E3.xls'; % Create a cell array to hold all header info % -- First create a 4x4 empty cell array head = cell(4,4); % -- Then fill cells with row and column headers head(1,:) = {'Sport Name \ Score', 'Wins', 'Loses','Ties'}; head(2:4,1) = {'University Soccer'; 'University Hockey'; 'University Baseball'}; % Define a matrix of 3 columns by 3 rows to hold the scores score = [12,7,2; 15,6,3; 10,5,4]; % Write the headers to the Excel file xlswrite (filename, head, 'Sheet1', 'A1'); % Write data to the Excel file xlswrite (filename, score, 'Sheet1', 'B2'); Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
28
Summary I/O Interactive I/O with input Output with disp, fprintf
I/O to/from *.mat files with save and load Writing to ASCII file with fprintf Importing text with uiimport Importing text with textscan Importing text with urlread Exporting to Excel file with xlswrite Importing from Excel file with uiimport Importing from Excel file with xlsread Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.