MATLAB® Data Import and Export R2011b How to Contact MathWorks www.mathworks.com Web comp.soft-sys.matlab Newsgroup www.mathworks.com/contact_TS.html TechnicalSupport [email protected] Productenhancementsuggestions [email protected] Bug reports [email protected] Documentationerrorreports [email protected] Orderstatus,licenserenewals,passcodes [email protected] Sales,pricing,andgeneralinformation 508-647-7000 (Phone) 508-647-7001 (Fax) The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 Forcontactinformationaboutworldwideoffices,seetheMathWorksWebsite. MATLAB®DataImportandExport ©COPYRIGHT2009–2011byTheMathWorks,Inc. Thesoftwaredescribedinthisdocumentisfurnishedunderalicenseagreement.Thesoftwaremaybeused orcopiedonlyunderthetermsofthelicenseagreement. Nopartofthismanualmaybephotocopiedor reproducedinanyformwithoutpriorwrittenconsentfromTheMathWorks,Inc. FEDERALACQUISITION:ThisprovisionappliestoallacquisitionsoftheProgramandDocumentation by,for,orthroughthefederalgovernmentoftheUnitedStates. ByacceptingdeliveryoftheProgram orDocumentation,thegovernmentherebyagreesthatthissoftwareordocumentationqualifiesas commercialcomputersoftwareorcommercialcomputersoftwaredocumentationassuchtermsareused ordefinedinFAR12.212,DFARSPart227.72,andDFARS252.227-7014. Accordingly,thetermsand conditionsofthisAgreementandonlythoserightsspecifiedinthisAgreement,shallpertaintoandgovern theuse,modification,reproduction,release,performance,display,anddisclosureoftheProgramand Documentationbythefederalgovernment(orotherentityacquiringfororthroughthefederalgovernment) andshallsupersedeanyconflictingcontractualtermsorconditions. IfthisLicensefailstomeetthe government’sneedsorisinconsistentinanyrespectwithfederalprocurementlaw,thegovernmentagrees toreturntheProgramandDocumentation,unused,toTheMathWorks,Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarksforalistofadditionaltrademarks. Otherproductorbrand namesmaybetrademarksorregisteredtrademarksoftheirrespectiveholders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patentsformoreinformation. Revision History September2009 Onlineonly NewforMATLAB7.9(Release2009b) March2010 Onlineonly RevisedforMATLAB7.10(Release2010a) September2010 Onlineonly RevisedforMATLAB7.11(Release2010b) April2011 Onlineonly RevisedforMATLAB7.12(Release2011a) September2011 Onlineonly RevisedforMATLAB7.13(Release2011b) Contents Supported File Formats 1 SupportedFileFormats ............................ 1-2 Importing Data 2 RecommendedMethodsforImportingData .......... 2-2 ToolsthatImportMultipleFileFormats ............... 2-2 ImportingSpecificFileFormats ...................... 2-3 ImportingDatainOtherFormats .................... 2-4 FindingFiles ..................................... 2-4 ProcessingaSequenceofFiles ....................... 2-5 TipsforUsingtheImportWizard .................... 2-5 ImportingMAT-Files ............................... 2-11 ViewtheContentsofaMAT-File ..................... 2-11 WaystoLoadDatafromaMAT-File .................. 2-12 LoadPartofaVariablefromaMAT-File .............. 2-13 Troubleshooting: LoadingVariableswithinaFunction ... 2-16 ImportingTextDataFiles .......................... 2-18 WaystoImportTextFiles .......................... 2-18 ImportNumericDatafromaTextFile ................ 2-20 ImportNumericDataandHeaderTextfromaTextFile .. 2-21 ImportMixedTextandNumericDatafromaTextFile ... 2-25 ImportLargeTextFiles ............................ 2-26 ImportDatafromaNonrectangularTextFile .......... 2-27 ImportTextDataFileswithLow-LevelI/O ............ 2-29 ImportingXMLDocuments ......................... 2-37 WhatIsanXMLDocumentObjectModel(DOM)? ....... 2-37 Example—FindingTextinanXMLFile .............. 2-38 iii ImportingSpreadsheets ............................ 2-41 WaystoImportSpreadsheets ........................ 2-41 SelectSpreadsheetDataInteractively ................. 2-42 ImportaWorksheetorRangewithxlsread ............. 2-44 ImportAllWorksheetsinaFilewithimportdata ........ 2-46 SystemRequirementsforImportingSpreadsheets ...... 2-47 WhentoConvertDatesfromExcelFiles ............... 2-48 ImportingScientificDataFiles ..................... 2-50 ImportingCommonDataFileFormat(CDF)Files ....... 2-50 Importing Network Common Data Form (NetCDF) Files .......................................... 2-57 Importing Flexible Image Transport System (FITS) Files .......................................... 2-64 ImportingHierarchicalDataFormat(HDF5)Files ...... 2-66 ImportingHierarchicalDataFormat(HDF4)Files ...... 2-73 ImportingImages .................................. 2-104 GettingInformationaboutImageFiles ................ 2-104 ReadingImageDataandMetadatafromTIFFFiles ..... 2-105 ImportingAudioandVideo ......................... 2-108 GettingInformationaboutAudioorVideoFiles ......... 2-108 ReadingAudioFiles ............................... 2-109 RecordingandPlayingAudio ........................ 2-109 ReadingVideoFiles ............................... 2-112 ImportingBinaryDatawithLow-LevelI/O ........... 2-117 Low-LevelFunctionsforImportingData .............. 2-117 ReadingBinaryDatainaFile ....................... 2-118 ReadingPortionsofaFile ........................... 2-120 ReadingFilesCreatedonOtherSystems .............. 2-123 OpeningFileswithDifferentCharacterEncodings ...... 2-124 Exporting Data 3 ExportingtoMAT-Files ............................ 3-2 WaystoSaveWorkspaceVariables ................... 3-2 iv Contents SavePartofaVariableinaMAT-File ................. 3-3 SaveStructureFieldsasSeparateVariables ........... 3-5 MAT-FileVersions ................................ 3-6 ExportingtoTextDataFiles ........................ 3-8 WritingtoDelimitedDataFiles ...................... 3-8 WritingtoaDiaryFile ............................. 3-11 WritingtoTextDataFileswithLow-LevelI/O .......... 3-12 ExportingtoXMLDocuments ....................... 3-20 CreatinganXMLFile .............................. 3-20 UpdatinganExistingXMLFile ...................... 3-22 ExportingtoExcelSpreadsheets .................... 3-24 WritingtoaSpreadsheetFile ....................... 3-24 AddingaNewWorksheet ........................... 3-24 FileFormatsthatxlswriteSupports .................. 3-25 ConvertingDates ................................. 3-25 FormattingCellsinExcelFiles ...................... 3-26 ExportingtoScientificDataFiles ................... 3-28 ExportingtoCommonDataFileFormat(CDF)Files ..... 3-28 Exporting to Network Common Data Form (NetCDF) Files .......................................... 3-30 ExportingtoHierarchicalDataFormat(HDF5)Files .... 3-39 ExportingtoHierarchicalDataFormat(HDF4)Files .... 3-49 ExportingtoImages ............................... 3-60 ExportingImageDataandMetadatatoTIFFFiles ...... 3-60 ExportingtoAudioandVideo ...................... 3-76 ExportingtoAudioFiles ............................ 3-76 ExportingVideotoAVIFiles ........................ 3-76 ExportingBinaryDatawithLow-LevelI/O ........... 3-78 Low-LevelFunctionsforExportingData .............. 3-78 WritingBinaryDatatoaFile ....................... 3-79 OverwritingorAppendingtoanExistingFile .......... 3-79 CreatingaFileforUseonaDifferentSystem .......... 3-81 OpeningFileswithDifferentCharacterEncodings ...... 3-82 WritingandReadingComplexNumbers ............... 3-83 v CreatingTemporaryFiles .......................... 3-86 Memory-Mapping Data Files 4 OverviewofMemory-Mapping ...................... 4-2 WhatIsMemory-Mapping? ......................... 4-2 BenefitsofMemory-Mapping ........................ 4-2 WhentoUseMemory-Mapping ...................... 4-4 MaximumSizeofaMemoryMap ..................... 4-5 ByteOrdering .................................... 4-6 ThememmapfileClass ............................. 4-7 SettingProperties ................................. 4-7 ViewingProperties ................................ 4-8 ConstructingamemmapfileObject .................. 4-10 HowtoRunExamplesinThisSection ................. 4-10 ConstructingtheObjectwithDefaultPropertyValues ... 4-11 ChangingPropertyValues .......................... 4-11 SelectingtheFiletoMap ........................... 4-13 SettingtheStartoftheMappedRegion ............... 4-14 IdentifyingtheContentsoftheMappedRegion ......... 4-14 MappingoftheExampleFile ........................ 4-19 RepeatingaFormatScheme ......................... 4-21 SettingtheTypeofAccess .......................... 4-22 ReadingaMappedFile ............................. 4-24 Introduction ...................................... 4-24 ImprovingPerformance ............................ 4-24 Example1—ReadingaSingleDataType ............. 4-25 Example2—FormattingFileDataasaMatrix ......... 4-26 Example3—ReadingMultipleDataTypes ............ 4-27 Example4—ModifyingMapParameters .............. 4-28 WritingtoaMappedFile ........................... 4-30 Example—WritingtoaMappedFile ................. 4-30 DimensionsoftheDataField ........................ 4-31 WritingMatricestoaMappedFile ................... 4-33 vi Contents SelectingAppropriateDataTypes .................... 4-35 WorkingwithCopiesoftheMappedData .............. 4-36 DeletingaMemoryMap ............................ 4-38 TheEffectofSharedDataCopiesOnPerformance ...... 4-38 Memory-MappingDemo ............................ 4-39 Introduction ...................................... 4-39 ThesendFunction ................................. 4-39 TheanswerFunction .............................. 4-41 RunningtheDemo ................................ 4-42 Internet File Access 5 DownloadingWebContentandFiles ................ 5-2 Example—UsingtheurlreadFunction ............... 5-2 Example—UsingtheurlwriteFunction ............... 5-3 CreatingandDecompressingZipArchives ........... 5-4 Example—UsingthezipFunction ................... 5-4 SendingEmail ..................................... 5-5 Example—UsingthesendmailFunction .............. 5-6 PerformingFTPFileOperations .................... 5-8 Example—RetrievingaFilefromanFTPServer ....... 5-8 Index vii viii Contents 1 Supported File Formats 1 Supported File Formats Supported File Formats The following table shows the file formats that you can import and export from the MATLAB® application. The Import Wizard and the importdata function support all listed formats, except: • Motion JPEG 2000. (cid:129) Platform-specific video. (cid:129) netCDF. (cid:129) H5. (cid:129) TheImportWizardsupportsHDFfiles,butimportdatadoesnot. Import Export File Content Extension Description Function Function MATLAB MAT Saved MATLAB load save formatted data workspace Text any White-space delimited load save-ascii numbers Delimited numbers dlmread dlmwrite Delimited numbers, or textscan a mix of strings and numbers Spreadsheet XLS Microsoft® Excel® xlsread xlswrite worksheet XLSX Formats supported on XLSB Windows® systems with XLSM Excel®2007orlater ODS OpenDocument™ xlsread none Spreadsheet, supported on Windows systems with Excel 2010 orlater (OpenDocument is a trademark of OASIS™, 1-2
Description: