Strategies for Efficient Use of Memory
In this section...
“Ways to Reduce the Amount of Memory Required” on page 29-2
“Using Appropriate Data Storage” on page 29-4
“How to Avoid Fragmenting Memory” on page 29-7
“Reclaiming Used Memory” on page 29-8
Ways to Reduce the Amount of Memory Required
The source of many "out of memory" problems often involves analyzing or processing an
existing large set of data such as in a file or a database. This requires bringing all or part
of the data set into the MATLAB software process. The following techniques deal with
minimizing the required memory during this stage.
Load Only as Much Data as You Need
Only import into MATLAB as much of a large data set as you need for the problem you are
trying to solve. This is not usually a problem when importing from sources such as a
database, where you can explicitly search for elements matching a query. But this is a
common problem with loading large flat text or binary files. Rather than loading the
entire file, use the appropriate MATLAB function to load parts of files.
File Type Partial Loading
MAT-file Load part of a variable by indexing into an
object that you create with the matfile
function.
Text Use the textscan function to access parts
of a large text file by reading only the
selected columns and rows. If you specify
the number of rows or a repeat format
number with textscan, MATLAB
calculates the exact amount of memory
required beforehand.
29 Memory Usage