MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

6 Data Import and Export Data Compression MATLAB compresses the data that you savetoaMAT-file.Datacompression can save you a significant amount of storage space when you are working with large files or working over a network. Data compression is optional, however, and you can disable it either for an individual save operation, or for all of your MATLAB sessions. Use the -v6 option with the save function to turn off compression on a per-command basis: save filename -v6 To disable data compression for all of your MATLAB sessions, open the Preferences dialog, select General and then MAT-Files, and click the option that is equivalent to the command save -v6. See General Preferences for MATLAB in the Desktop Tools and Development Environment documentation for more information. Note You cannot read a compressed MAT-file with MATLAB versions earlier thanVersion7.TowriteaMAT-filethatyouwillbeabletoreadwithoneof these versions, save to the file with data compression disabled. Information returned by the command whos -file is independent of whether the variables in that file are compressed or not. The byte counts returned by this command represent the number of bytes data occupies in the MATLAB workspace, and not in the file the data was saved to. Evaluating When to Compress. You should consider both data set size and the type of data being saved when deciding whether or not to compress the data you save to a file. The benefits of data compression are greater when saving large data sets (over 3 MB), and are usually negligible with smaller data sets. Data that has repeating patterns or more consistent values compresses better than random data. Compressing data that has a random pattern is not recommended as it slows down the performance of save and load significantly, and offers little benefit in return. In general, data compression and decompression slows down all save and some load operations to some extent. In most cases, however, the resulting reductioninfilesizeisworththeadditional time spent compressing or 6-26

Saving and Loading MAT-Files decompressing. Because loading is typically done more frequently than saving, load is considered to be the most critical 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 same data. Beyond that threshold, however, loading the compressed file is faster. For example, say that you have a block of data that takes up 100 MB in memory, and this data has been saved to both a 10 MB compressed file and a 100 MB uncompressed file. When you 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 90 MB from the uncompressed file will take 9 times as long as the first 10 MB, while all that remains to be done 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 computer being used. Network users loading compressed MAT-files generally see faster load times than when loading uncompressed files, and at smaller data sizes than users loading the same files locally. Note Compression and decompression during save and load is done transparently without the use 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 encoding. As with data compression, Unicode character encoding is optional. If you disable it, MATLAB writes the MAT-file using the default encoding for your system. To disable Unicode character encoding on a per-command basis, use the -v6 option with the save function: save filename -v6 To disable Unicode character encoding for all of your MATLAB sessions, open the Preferences dialog, select General and then MAT-Files, and click the option that is equivalent to the command save -v6. SeeGeneral 6-27

Saving and Loading MAT-Files<br />

decompressing. Because loading is typically done more frequently than<br />

saving, load is considered to be the most critical of the two operations. Up to a<br />

certain threshold (relative to the size of the uncompressed MAT-file), loading<br />

a compressed MAT-File is slightly slower than loading an uncompressed<br />

file containing the same data. Beyond that threshold, however, loading the<br />

compressed file is faster.<br />

For example, say that you have a block of data that takes up 100 MB in<br />

memory, and this data has been saved to both a 10 MB compressed file and a<br />

100 MB uncompressed file. When you load each of these files back into the<br />

<strong>MATLAB</strong> workspace, the first 10 MB of data takes the same amount of time<br />

to load for each file. Loading the remaining 90 MB from the uncompressed<br />

file will take 9 times as long as the first 10 MB, while all that remains to be<br />

done with the compressed file is to decompress the data, and this takes a<br />

relatively short amount of time.<br />

The loading size threshold is lower for network files, and also varies depending<br />

on the type of computer being used. Network users loading compressed<br />

MAT-files generally see faster load times than when loading uncompressed<br />

files, and at smaller data sizes than users loading the same files locally.<br />

Note Compression and decompression during save and load is done<br />

transparently without the use of temporary files on disk. This is of<br />

significance to large dataset users in particular.<br />

Unicode Character Encoding<br />

<strong>MATLAB</strong> saves character data to a MAT-file using Unicode character<br />

encoding. As with data compression, Unicode character encoding is optional.<br />

If you disable it, <strong>MATLAB</strong> writes the MAT-file using the default encoding for<br />

your system. To disable Unicode character encoding on a per-command basis,<br />

use the -v6 option with the save function:<br />

save filename -v6<br />

To disable Unicode character encoding for all of your <strong>MATLAB</strong> sessions,<br />

open the Preferences dialog, select General and then MAT-Files, and<br />

click the option that is equivalent to the command save -v6. SeeGeneral<br />

6-27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!