MATLAB Data Import and Export

[Pages:408]MATLAB? Data Import and Export

R2014b

How to Contact MathWorks

Latest news: Sales and services: User community: Technical support: Phone:

sales_and_services matlabcentral support/contact_us

508-647-7000

The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098

MATLAB? Data Import and Export

? COPYRIGHT 2009?2014 by The MathWorks, Inc.

The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc.

FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.

Trademarks

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

Patents

MathWorks products are protected by one or more U.S. patents. Please see patents for more information.

Revision History

September 2009 March 2010 September 2010 April 2011 September 2011 March 2012 September 2012 March 2013 September 2013 March 2014 October 2014

Online only Online only Online only Online only Online only Online only Online only Online only Online only Online only Online only

New for MATLAB 7.9 (Release 2009b) Revised for Version 7.10 (Release 2010a) Revised for Version 7.11 (Release 2010b) Revised for Version 7.12 (Release 2011a) Revised for Version 7.13 (Release 2011b) Revised for Version 7.14 (Release 2012a) Revised for Version 8.0 (Release 2012b) Revised for Version 8.1 (Release 2013a) Revised for Version 8.2 (Release 2013b) Revised for Version 8.3 (Release 2014a) Revised for Version 8.4 (Release 2014b)

Contents

File Opening, Loading, and Saving

1

Supported File Formats for Import and Export . . . . . . . . . . 1-2

Methods for Importing Data . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 Tools that Import Multiple File Formats . . . . . . . . . . . . . . . . 1-6 Importing Specific File Formats . . . . . . . . . . . . . . . . . . . . . . 1-6 Importing Data with Low-Level I/O . . . . . . . . . . . . . . . . . . . 1-7

Import Images, Audio, and Video Interactively . . . . . . . . . . . Viewing the Contents of a File . . . . . . . . . . . . . . . . . . . . . . . Specifying Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generating Reusable MATLAB Code . . . . . . . . . . . . . . . . . .

1-8 1-8 1-9 1-10

Import or Export a Sequence of Files . . . . . . . . . . . . . . . . . . 1-12

View the Contents of a MAT-File . . . . . . . . . . . . . . . . . . . . . . 1-13

Load Parts of Variables from MAT-Files . . . . . . . . . . . . . . . Load Using the matfile Function . . . . . . . . . . . . . . . . . . . . . Load from Variables with Unknown Names . . . . . . . . . . . . Avoid Repeated File Access . . . . . . . . . . . . . . . . . . . . . . . . . Avoid Inadvertently Loading Entire Variables . . . . . . . . . . Partial Loading Requires Version 7.3 MAT-Files . . . . . . . . .

1-14 1-14 1-15 1-16 1-16 1-17

Save Parts of Variables to MAT-Files . . . . . . . . . . . . . . . . . . Save Using the matfile Function . . . . . . . . . . . . . . . . . . . . . Partial Saving Requires Version 7.3 MAT-Files . . . . . . . . . .

1-18 1-18 1-19

Save Structure Fields as Separate Variables . . . . . . . . . . . . 1-21

MAT-File Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Default Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overriding the Default MAT-File Version . . . . . . . . . . . . . .

1-22 1-22 1-22

v

Speeding Up Save and Load Operations . . . . . . . . . . . . . . . File Size Increases Unexpectedly When Growing Array . . . Loading Variables within a Function . . . . . . . . . . . . . . . . . . Create Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1-23 1-25 1-27 1-28

Text Files

2

Ways to Import Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2

Select Text File Data Using Import Tool . . . . . . . . . . . . . . . . 2-4 Select Data Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 Import Data from Multiple Text Files . . . . . . . . . . . . . . . . . . 2-7

Import Dates and Times from Text Files . . . . . . . . . . . . . . . . 2-9

Import Numeric Data from Text Files . . . . . . . . . . . . . . . . . Import Comma-Separated Data . . . . . . . . . . . . . . . . . . . . . . Import Delimited Numeric Data . . . . . . . . . . . . . . . . . . . . .

2-11 2-11 2-12

Import Mixed Data from Text Files . . . . . . . . . . . . . . . . . . . . 2-14

Import Large Text File Data in Blocks . . . . . . . . . . . . . . . . . 2-18

Import Data from a Nonrectangular Text File . . . . . . . . . . . 2-25

Write to Delimited Data Files . . . . . . . . . . . . . . . . . . . . . . . . Export Numeric Array to ASCII File . . . . . . . . . . . . . . . . . . Export Table to Text File . . . . . . . . . . . . . . . . . . . . . . . . . . Export Cell Array to Text File . . . . . . . . . . . . . . . . . . . . . .

2-27 2-27 2-28 2-30

Write to a Diary File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33

vi Contents

Spreadsheets

3

Ways to Import Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Import Data from Spreadsheets . . . . . . . . . . . . . . . . . . . . . . 3-2 Paste Data from Clipboard . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2

Select Spreadsheet Data Using Import Tool . . . . . . . . . . . . . 3-4 Select Data Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4 Import Data from Multiple Spreadsheets . . . . . . . . . . . . . . . 3-6

Import a Worksheet or Range . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 Read Column-Oriented Data into Table . . . . . . . . . . . . . . . . 3-7 Read Numeric and Text Data into Arrays . . . . . . . . . . . . . . . 3-8 Get Information about a Spreadsheet . . . . . . . . . . . . . . . . . . 3-9

Import All Worksheets from a File . . . . . . . . . . . . . . . . . . . . Import Numeric Data from All Worksheets . . . . . . . . . . . . . Import Data and Headers from All Worksheets . . . . . . . . . .

3-11 3-11 3-11

System Requirements for Importing Spreadsheets . . . . . . . Importing Spreadsheets with Excel for Windows . . . . . . . . . Importing Spreadsheets Without Excel for Windows . . . . . .

3-14 3-14 3-14

Import and Export Dates to Excel Files . . . . . . . . . . . . . . . . MATLAB and Excel Dates . . . . . . . . . . . . . . . . . . . . . . . . . Import Dates on Systems with Excel for Windows . . . . . . . . Import Dates on Systems Without Excel for Windows . . . . . Export Dates to Excel File . . . . . . . . . . . . . . . . . . . . . . . . .

3-15 3-15 3-15 3-16 3-17

Export to Excel Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . Write Tabular Data to Spreadsheet File . . . . . . . . . . . . . . . Write Numeric and Text Data to Spreadsheet File . . . . . . . Disable Warning When Adding New Worksheet . . . . . . . . . Supported Excel File Formats . . . . . . . . . . . . . . . . . . . . . . . Format Cells in Excel Files . . . . . . . . . . . . . . . . . . . . . . . . .

3-20 3-20 3-21 3-22 3-22 3-22

vii

Low-Level File I/O

4

Import Text Data Files with Low-Level I/O . . . . . . . . . . . . . . 4-2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Reading Data in a Formatted Pattern . . . . . . . . . . . . . . . . . . 4-3 Reading Data Line-by-Line . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5 Testing for End of File (EOF) . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Opening Files with Different Character Encodings . . . . . . . . 4-9

Import Binary Data with Low-Level I/O . . . . . . . . . . . . . . . . Low-Level Functions for Importing Data . . . . . . . . . . . . . . . Reading Binary Data in a File . . . . . . . . . . . . . . . . . . . . . . Reading Portions of a File . . . . . . . . . . . . . . . . . . . . . . . . . . Reading Files Created on Other Systems . . . . . . . . . . . . . . Opening Files with Different Character Encodings . . . . . . .

4-10 4-10 4-11 4-13 4-16 4-16

Export to Text Data Files with Low-Level I/O . . . . . . . . . . . Writing to Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appending or Overwriting Existing Files . . . . . . . . . . . . . . Opening Files with Different Character Encodings . . . . . . .

4-18 4-18 4-20 4-23

Export Binary Data with Low-Level I/O . . . . . . . . . . . . . . . . Low-Level Functions for Exporting Data . . . . . . . . . . . . . . . Writing Binary Data to a File . . . . . . . . . . . . . . . . . . . . . . . Overwriting or Appending to an Existing File . . . . . . . . . . . Creating a File for Use on a Different System . . . . . . . . . . . Opening Files with Different Character Encodings . . . . . . . Writing and Reading Complex Numbers . . . . . . . . . . . . . . .

4-25 4-25 4-25 4-26 4-28 4-29 4-29

Images

5

Importing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Getting Information about Image Files . . . . . . . . . . . . . . . . . 5-2 Reading Image Data and Metadata from TIFF Files . . . . . . . 5-3

Exporting to Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 Exporting Image Data and Metadata to TIFF Files . . . . . . . . 5-5

viii Contents

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download