How to Read Data File in Matlab

Saving and Loading MAT-Files

This section explains how to save the variables in your MATLAB session to a binary file called a MAT-file, and how to load them back into your MATLAB workspace. It covers the following:

  • Exporting Data to MAT-Files
  • Importing Data from MAT-Files

MAT-files are double-precision, binary, MATLAB format files. They tin exist created on one machine and after read by MATLAB on another machine with a dissimilar floating-signal format, retaining as much accuracy and range as the different formats allow. They tin can too exist manipulated by other programs external to MATLAB.

Exporting Data to MAT-Files

This section covers

  • Using the relieve Function
  • Saving Structures
  • Appending to an Existing File
  • Information Compression
  • Unicode Character Encoding
  • Optional Output Formats
  • Storage Requirements
  • Saving from External Programs

Using the save Function

To export workspace variables to a binary or ASCII file, use the save function. You can relieve all variables from the workspace in a single operation (if you omit the filename, MATLAB uses the name matlab.mat):

  • save                          filename                                    

or salvage merely those variables that y'all specify:

  • save                          filename                                      var1                                      var2                        ...                          varN                                    

Apply the wildcard character (*) in the variable name to save those variables that friction match a specific pattern. For example, the following command saves all variables that start with str.

  • save strinfo str*          

Use whos -file to examine what has been written to the MAT-file:

  • whos -file strinfo   Name           Size                   Bytes  Class    str2           1x15                      xxx  char array   strarray       2x5                      678  cell array   strlen         1x1                        viii  double array          

Saving Structures

When saving a MATLAB structure, you lot have the option of saving the entire structure, saving each structure field as an private variable in the MAT-file, or saving specific fields as individual variables.

For structure S,

  • S.a = 12.seven;  Southward.b = {'abc', [4 5; 6 7]};  S.c = 'Hullo!';          

Relieve the unabridged structure to newstruct.mat with the usual syntax:

  • salvage newstruct.mat S;  whos -file newstruct   Name      Size                   Bytes  Form    S         1x1                      550  struct array          

Save the fields individually with the -struct option:

  • relieve newstruct.mat -struct S;  whos -file newstruct   Name      Size                   Bytes  Class    a         1x1                        8  double assortment   b         1x2                      158  jail cell array   c         1x6                       12  char assortment          

Or save only selected fields using -struct and specifying each field proper noun:

  • relieve newstruct.mat -struct Due south a c;  whos -file newstruct   Proper noun      Size                   Bytes  Class    a         1x1                        8  double array   c         1x6                       12  char array          

Appending to an Existing File

You can add new variables to those already stored in an existing MAT-file by using salve -append. When you append to a MAT-file, MATLAB first looks in the designated file for each variable proper noun specified in the statement list, or for all variables if no specific variable names are specified. Based on that information, MATLAB does both of the post-obit:

  • For each variable that already exists in the MAT-file, MATLAB overwrites its saved value with the new value taken from the workspace.
  • For each variable not found in the MAT-file, MATLAB adds that variable to the file and stores its value from the workspace.

    Note    Saving with the -append option does not suspend additional elements to whatsoever arrays that are already saved in the MAT-file.

Data Compression

MATLAB compresses the information that y'all save to a MAT-file. Information pinch tin save yous a significant amount of storage space when you are working with large files or working over a network.

Information compression is optional, however, and yous can disable it either for an private relieve operation, or for all of your MATLAB sessions. Use the -v6 pick with the save part to plow off pinch on a per-control basis:

  • salvage filename -v6          

To disable data compression for all of your MATLAB sessions, open up the Preferences dialog, select Full general and then MAT-Files, and click Save uncompressed. Meet Full general Preferences for MATLAB in the Desktop Tools and Evolution Environs documentation for more than information.

    Notation    You lot cannot read a compressed MAT-file with MATLAB Version 6 or 6.v. To write a MAT-file that yous will exist able to read with one of these versions, save to the file with data compression disabled.

Information returned by the command whos -file is contained of whether the variables in that file are compressed or not. The byte counts returned by this command stand for the number of bytes information occupies in the MATLAB workspace, and non in the file the data was saved to.

Evaluating When to Compress. You should consider both data prepare size and the type of data being saved when deciding whether or not to shrink the information you salve to a file. The benefits of data compression are greater when saving big information sets (over 3MB), and are unremarkably negligible with smaller data sets. Data that has repeating patterns or more consequent values compresses better than random data. Compressing data that has a random pattern is not recommended equally it slows down the performance of save and load significantly, and offers little benefit in render.

In general, data compression and decompression slows down all save and some load operations to some extent. In nearly cases, however, the resulting reduction in file size is worth the boosted time spent compressing or decompressing. Because loading is typically done more frequently than saving, load is considered to be the most disquisitional of the two operations. Up to a certain threshold (relative to the size of the uncompressed MAT-file), loading a compressed MAT-File is slightly slower than loading an uncompressed file containing the aforementioned data. Beyond that threshold, however, loading the compressed file is faster.

For case, say that yous accept a block of data that takes up 100 MB in retentiveness, and this data has been saved to both a 10 MB compressed file and a 100 MB uncompressed file. When you lot load each of these files back into the MATLAB workspace, the first 10 MB of data takes the same amount of time to load for each file. Loading the remaining ninety MB from the uncompressed file will take 9 times as long every bit the offset 10 MB, while all that remains to be washed with the compressed file is to decompress the data, and this takes a relatively short amount of time.

The loading size threshold is lower for network files, and also varies depending on the type of reckoner being used. Network users loading compressed MAT-files generally see faster load times than when loading uncompressed files, and at smaller information sizes than users loading the same files locally.

    Notation    Compression and decompression during salvage and load is done transparently without the utilize of temporary files on disk. This is of significance to large dataset users in particular.

Unicode Character Encoding

MATLAB saves character data to a MAT-file using Unicode character data encoding. As with information compression, Unicode encoding is optional. If you disable it, MATLAB writes the MAT-file using the default character set for your arrangement. To disable Unicode character encoding on a per-command basis, use the -v6 pick with the salvage part:

  • save filename -v6          

To disable Unicode encoding for all of your MATLAB sessions, open the Preferences dialog, select Full general and and then MAT-Files, and click Local Character Set. See Full general Preferences for MATLAB in the Desktop Tools and Development Surround documentation for more information. When writing character data using Unicode encoding (the default), MATLAB checks if the data is 7-flake ASCII. If it is, MATLAB writes the 7-bit ASCII graphic symbol data to the MAT-file using viii $.25 per character (UTF-8 format), thus minimizing the size of the resulting file. Whatsoever graphic symbol data that is non 7-bit ASCII is written in xvi-flake Unicode course (UTF-16). This algorithm operates on a per-string ground.

    Note    Y'all cannot read a Unicode encoded MAT-file with MATLAB Version 6 or six.5. To write a MAT-file that you will be able to read with one of these versions, save to the file with Unicode character encoding disabled.

For more information on how MATLAB saves specific ASCII information formats, and on preventing loss or corruption of character data, run across Writing Grapheme Data in the MATLAB External Interfaces documentation.

Optional Output Formats

You lot can choose from whatsoever of the following formats for your output file. If yous do not specify a format, MATLAB uses the binary MAT-file format.

Output File Format
Command
Binary MAT-file (default)
salvage filename
eight-digit ASCII
save filename -ascii
8-digit ASCII, tab delimited
save filename -ascii -tabs
16-digit ASCII
save filename -ascii -double
16-digit ASCII, tab delimited
save filename -ascii -double -tabs
MATLAB Version four compatible
save filename -v4

Saving in ASCII Format. When saving in any of the ASCII formats, consider the following:

  • Each variable to be saved must be either a ii-dimensional double array or a 2-dimensional character array. Saving a circuitous double array causes the imaginary part of the information to exist lost, equally MATLAB cannot load nonnumeric data ('i').
  • To read the file with the MATLAB load function, make sure all the variables have the same number of columns. If you lot are using a plan other than MATLAB to read the saved data, this restriction can be relaxed.
  • Each MATLAB graphic symbol in a grapheme array is converted to a floating-indicate number equal to its internal ASCII code and written out as a floating-indicate number string. At that place is no information in the saved file that indicates whether the value was originally a number or a grapheme.
  • The values of all variables saved merge into a unmarried variable that takes the proper noun of the ASCII file (minus any extension). Therefore, it is advisable to save only one variable at a time.

Saving in Version iv Format. With the -v4 option, you can salve only those data constructs that are uniform with MATLAB Version 4. Therefore, you cannot save structures, jail cell arrays, multidimensional arrays, or objects. Variable names cannot exceed 19 characters in length. In improver, you must use filenames that are supported by MATLAB Version 4.

Storage Requirements

The binary formats used by salve depend on the size and type of each array. Arrays with any noninteger entries and arrays with ten,000 or fewer elements are saved in floating-signal formats requiring 8 bytes per existent element. Arrays with all integer entries and more than than x,000 elements are saved in the formats shown, requiring fewer bytes per element.

Element Range
Bytes per Element
0 to 255
1
0 to 65535
2
-32767 to 32767
2
-231 to 231-1
4
Other
viii

Saving from External Programs

The MATLAB External Interfaces documentation provides details on reading and writing MAT-files from external C or Fortran programs. Information technology is important to use recommended admission methods, rather than rely upon the specific MAT-file format, which is likely to change in the future.

Previous page  Supported File Formats Importing Information from MAT-Files Next page

© 1994-2005 The MathWorks, Inc.


pullenwhippyraton1972.blogspot.com

Source: http://matlab.izmiran.ru/help/techdoc/matlab_prog/ch10_in7.html

0 Response to "How to Read Data File in Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel