This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Read data in datastore


data = read(ds)
[data,info] = read(ds)



data = read(ds) returns data from a datastore. Subsequent calls to the read function continue reading from the endpoint of the previous call.

[data,info] = read(ds) also returns information about the extracted data in info, including metadata.


collapse all

Create a datastore from the sample file, airlinesmall.csv, which contains tabular data.

ds = tabularTextDatastore('airlinesmall.csv','TreatAsMissing','NA','MissingValue',0);

Modify the SelectedVariableNames property to specify the variables of interest.

ds.SelectedVariableNames = {'DepTime','ArrTime','ActualElapsedTime'};

While there is data available to be read from the datastore, read one block of data at a time and analyze the data. In this example, sum the actual elapsed time.

sumElapsedTime = 0;
while hasdata(ds)
    T = read(ds);
    sumElapsedTime = sumElapsedTime + sum(T.ActualElapsedTime);

View the sum of the actual elapsed time.

sumElapsedTime = 14531797

Create a datastore from the sample file, mapredout.mat, which is the output file of the mapreduce function.

ds = datastore('mapredout.mat');

Read a subset of data in the datastore.

T = read(ds)
T=1×2 table
    Key      Value 
    ____    _______

    'AA'    [14930]

Change the number of key-value pairs to read at a time, by changing the ReadSize property of the datastore.

ds.ReadSize = 5;

Read the next five key-value pairs in the datastore.

T = read(ds)
T=5×2 table
    Key      Value 
    ____    _______

    'AS'    [ 2910]
    'CO'    [ 8138]
    'DL'    [16578]
    'EA'    [  920]
    'HP'    [ 3660]

Input Arguments

collapse all

Input datastore. You can use the datastore function to create a datastore object from your data.

Output Arguments

collapse all

Output data, returned as a table or an array depending on the type of ds.

Type of DatastoreData type of dataDescription
TabularTextDatastore and SpreadsheetDatastoreTableThe SelectedVariableNames property determines the table variables.
ImageDatastoreInteger array The dimensions of the integer array depend on the type of image:
  • For grayscale images, data is m-by-n.

  • For truecolor images, data is m-by-n-by-3.

  • For CMYK Tiff images, data is m-by-n-by-4.

If the ReadSize property is greater than 1, then data is a cell array of image data corresponding to each image. The read function supports all image types supported by the imread function. For more information on the supported image types, see imread.
KeyValueDatastoreTableThe table variable names are Key and Value.
FileDatastoreVariesThe output is the same as the output returned by the custom read function, specified by the 'ReadFcn' value.

Information about read data, returned as a structure array. The structure array can contain the following fields.

Field NameDatastore TypesDescription
FilenameAllFilename is a fully resolved path containing the path string, name of the file, and file extension. For ImageDatastore objects whose ReadSize property is greater than 1, Filename is a cell array of file names corresponding to each image.

Total file size, in bytes. For MAT-files, FileSize is the total number of key-value pairs in the file. For ImageDatastore objects whose ReadSize property is greater than 1, FileSize is a vector of file sizes corresponding to each image.

FileTypeKeyValueDatastore only

The type of file from which data is read, either 'mat' for MAT-files or 'seq' for sequence files.

LabelImageDatastore only

Image label name. If the ReadSize property is greater than 1, then Label is a vector of label names corresponding to each image. If the Labels property is empty, then Label is an empty cell array.

NumCharactersReadTabularTextDatastore only

Number of characters read.

NumDataRowsSpreadsheetDatastore only

Vector containing number of rows read from each sheet.

OffsetKeyValueDatastore and TabularTextDatastore only

Starting position of the read operation, in bytes. For MAT-files, Offset is the index of the first key and value read.

SheetNamesSpreadsheetDatastore only

Names of sheets read.

SheetNumbersSpreadsheetDatastore only

Numbering associated with sheets read.

Introduced in R2014b