Using MATLAB (779505), страница 22
Текст из файла (страница 22)
Then, read the section referred to in the table.If you are familiar with MATLAB export function but not sure when to usethem, view Table 6-4, which compares the features of each function.Note If C or Fortran routines for writing data files in the form needed byother applications exist, create a MEX file to write the data. See the ExternalInterfaces Guide for more information.Table 6-3: ASCII Data File Formats and MATLAB Export Commands6-16Data Format SampleMATLAB Export Function1 2 3 4 56 7 8 9 10See “Exporting Delimited ASCII Data Files” on page 6-17 and“Using the diary Command to Export Data” on page 6-18 forinformation about these options.1; 2; 3; 4; 5;6; 7; 8; 9; 10;See “Exporting Delimited ASCII Data Files” on page 6-17 formore information. The example shows a semicolon-delimited filebut you can specify other characters as the delimiter.Exporting ASCII DataTable 6-4: ASCII Data Export Function Feature ComparisonFunctionUse WithDelimitersNotescsvwriteNumeric dataOnlycommasPrimarily used with spreadsheet data.See also the binary format spreadsheetexport functionsdiaryNumeric data orcell arrayOnly spacesCan be used for small arrays.
Requiresediting of data file to remove extraneoustext.dlmwriteNumeric dataAnycharacterEasy to use, flexible.fprintfAlphabetic andnumeric dataAnycharacterPart of low-level file I/O routines. Mostflexible command but most difficult touse. Requires use of fopen to obtain fileidentifier and fclose after write.saveNumeric dataTabs orspacesEasy to use; output values are highprecision.Exporting Delimited ASCII Data FilesTo export an array as a delimited ASCII data file, you can use either the savecommand, specifying the -ASCII qualifier, or the dlmwrite function. The savecommand is easy to use; however, the dlmwrite function provides moreflexibility, allowing you to specify any character as a delimiter and to exportsubsets of an array by specifying a range of values.Using the save CommandTo export the array A,A = [ 1 2 3 4 ; 5 6 7 8 ];use the save command, as follows.save my_data.out A -ASCIIIf you view the created file in a text editor, it looks like this.6-176Importing and Exporting Data1.0000000e+0005.0000000e+0002.0000000e+0006.0000000e+0003.0000000e+0007.0000000e+0004.0000000e+0008.0000000e+000By default, save uses spaces as delimiters but you can use tabs instead ofspaces by specifying the -tabs qualifier.When you use save to write a character array to an ASCII file, it writes theASCII equivalent of the characters to the file.
If you write the character string'hello' to a file, save writes the values104 101 108 108 111Using the dlmwrite FunctionTo export an array in ASCII format and specify the delimiter used in the file,use the dlmwrite function.For example, to export the array A,A = [ 1 2 3 4 ; 5 6 7 8 ];as an ASCII data file that uses semicolons as a delimiter, use this commanddlmwrite('my_data.out',A, ';')If you view the created file in a text editor, it looks like this1;2;3;4 5;6;7;8Note that dlmwrite does not insert delimiters at the end of rows.By default, if you do not specify a delimiter, dlmwrite uses commas as adelimiter. You can specify a space (’ ’) as a delimiter or, if you specify emptyquotes (’’), no delimiter.Using the diary Command to Export DataTo export small numeric arrays or cell arrays, you can use the diary command.diary creates a verbatim copy of your MATLAB session in a disk file (excludinggraphics).For example, if you have the array, A, in your workspaceA = [ 1 2 3 4; 5 6 7 8 ];6-18Exporting ASCII Dataexecute these commands at the MATLAB prompt to export this array usingdiary:1 Turn on the diary function.
You can optionally name the output file diarycreates.diary my_data.out2 Display the contents of the array you want to export. This example displaysthe array A. You could also display a cell array or other MATLAB data type.A =152637483 Turn off the diary function.diary offdiary creates the file, my_data.out, and records all the commands executedin the MATLAB session until it is turned off.A =15263748diary off4 Open the diary file, my_data.out, in a text editor and remove all theextraneous text.6-196Importing and Exporting DataImporting Binary DataThe easiest way to import binary data is by using the Import Wizard.
Yousimply start the Import Wizard and specify the file that contains the data youwant to import. For more information, see “Using the Import Wizard withBinary Data Files” on page 6-20.If you need to work from the MATLAB command line or perform importoperations as part of an M-file, you must use one of the MATLAB importfunctions. MATLAB supports many functions to import data in different binaryformats, such as image files or spreadsheet data files. Your choice of whichfunction to use depends on the type of data in the file and how the data isformatted.Caution When you import data into the MATLAB workspace, it overwritesany existing variables in the workspace with the same name.Using the Import Wizard with Binary Data FilesTo import text data using the Import Wizard, perform these steps:1 Start the Import Wizard, by selecting the Import Data option on theMATLAB File menu.
MATLAB displays a file selection dialog box. You canalso use the uiimport function to start the Import Wizard.To use the Import Wizard to import data from the clipboard, select the PasteSpecial option on the MATLAB Edit menu. You can also right-click in theMATLAB command window and choose Paste Special from the contextmenu. Skip to step 3 to continue importing from the clipboard.2 Specify the file you want to import in the file selection dialog box and clickOpen. The Import Wizard opens the file and attempts to process itscontents.
See Viewing the variables for more information.3 Select the variables that you want to import. By default, the Import Wizardcreates variables depending on the type of data in the file.4 Click Finish to import the data into the workspace.6-20Importing Binary DataViewing the VariablesWhen the Import Wizard opens a binary data file, it attempts to process thedata in the file, creating variables from the data it finds in the file.For example, if you use the Import Wizard to import this sample MAT-file,my_data.mat,A =1 2 3 4 56 7 8 9 10B =a test stringit creates two variables, listed in the Preview pane. You can select the variablesyou want to import by clicking in the check box next to its name.
All variablesare preselected by default.Preview of thevariables in thefile.Preview of thedata in eachvariable.For other binary data types, such as images and sound files, the Import Wizarddisplays information about the data in the left pane and provides a Previewbutton in the right pane of the dialog box. Click the preview button to view (orlisten to) the data.6-216Importing and Exporting DataFor example, when used to import a movie in Audio Video Interleaved (AVI)format, the Import Wizard displays this dialog box..Information aboutthe data.Preview button.Using Import Functions with Binary DataTo import binary data from the command line or in an M-file, you must use oneof the MATLAB import functions.
Your choice of function depends on how thedata in the text file is formatted.To find the function designed to work with a particular binary data format,scan the data formats listed in Table 6-5. The table lists the binary formats andthe MATLAB high-level functions you use to import them, along with pointersto additional information.6-22Importing Binary DataTable 6-5: Binary Data Formats and MATLAB Import FunctionsData FormatFileExtensionDescriptionAudio files.au.wavUse the auread function to import audio files on SunMicrosystems platforms and the wavread function toimport audio files on Microsoft Windows systems.Audio-videoInterleaved (AVI).aviUse the aviread function to import Audio-videoInterleaved (AVI) files.Hierarchical DataFormat (HDF).hdfFor HDF image files, use imread.
For all other HDF files,see “Working with HDF Data” on page 6-29 for completeinformation.Image files.jpeg.tiff.bmp.gif.png.hdf.pcx.xwdUse the imread function to import image data in manydifferent formats. See Reading, Writing, and QueryingGraphics Image Files for more information.MATLABproprietary format(MAT-files).matUse the load command to import data in MATLABproprietary format. See “Loading a Saved Workspace” onpage 5-7 for more information.Spreadsheets.xls.wk1Use xlsread to import Excel spreadsheet data or wk1readto import Lotus 123 data.To view the alphabetical list of MATLAB binary data export functions, seeTable 6-6.If MATLAB does not support a high-level function that works with a dataformat, you can use the MATLAB low-level file I/O functions, if you know howthe binary data is formatted in the file.
See “Using Low-Level File I/OFunctions” on page 6-51 for more information.6-236Importing and Exporting DataTable 6-6: Binary Data Import Functions6-24FunctionFile ExtensionData Formatauread.auImport sound data in Sun Microsystems format.aviread.aviImport audio-visual data in AVI format.hdf.hdfImport data in Hierarchical Data Format (HDF). ForHDF image file formats, use imread. For all otherHDF files, see “Working with HDF Data” on page6-29 for complete information.imread.jpeg, .tiff, .bmp,.png, .hdf, .pcx,.xwd, .gifImport images in many formats.load.matImport MATLAB workspace variables in MAT-filesformat.wavread.wavImport sound data in Microsoft Windows format.wk1read.wk1Import data in Lotus 123 spreadsheet format.xlsread.xlsImport data in Microsoft Excel spreadsheet format.Exporting Binary DataExporting Binary DataTo export binary data in one of the standard binary formats, you can use theMATLAB high-level function designed to work with that format.To find the function designed to work with a particular binary data format,scan the data formats listed in Table 6-7.















