Main Content

seek

Class: matlab.io.datastore.DsFileReader
Namespace: matlab.io.datastore

Seek to a position in the file

Syntax

numBytes = seek(fr,n)
numBytes = seek(fr,n,Name,Value)

Description

numBytes = seek(fr,n) moves the file position indicator by n bytes past current position in the file specified by the fr object. seek returns the actual number of bytes by which the position indicator was moved.

numBytes = seek(fr,n,Name,Value) specifies additional parameters using one or more name-value pair arguments. For example, you can specify the starting position of the seek operation by specifying 'Origin','start-of-file'.

Input Arguments

expand all

File-reader object, specified as a matlab.io.datastore.DsFileReader object. To create a DsFileReader object, see matlab.io.datastore.DsFileReader.

Number of bytes, specified as an integer. The seek method moves the file position indicator n bytes from current position in the specified file. If n is negative, seek will move the position indicator backwards in the file.

Example: seek(fr,20)

Data Types: double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'RespectTextEncoding',true

Respect character boundaries, specified as the comma-separated pair consisting of 'RespectTextEncoding' followed by true or false.

  • true - Respect character boundaries of multibyte characters.

  • false - Do not respect character boundaries of multibyte characters.

Example: 'RespectTextEncoding',true

Data Types: logical

Starting position, specified as the comma-separated pair consisting of 'Origin' followed by one of these values.

  • 'currentposition' - Start the seek operation from the current position indicator in the file.

  • 'start-of-file' - Starts the seek operation from position zero.

  • 'end-of-file' - Starts the seek operation from the end of the file.

Example: 'Origin','start-of-file'

Data Types: char | string

Examples

Seek to Position in File and Read

Create a file-reader object for a file, seek to the desired starting position, and read a portion of the file.

Create a DsFileReader object for airlinesmall.csv.

fr = matlab.io.datastore.DsFileReader('airlinesmall.csv');

The airlinesmall.csv file has variable names at the beginning of the file. The variable names line ends at the position marked by 299 bytes. To get past the variable names line, use the seek method to move the read pointer to the starting position.

seek(fr,299,'RespectTextEncoding',true);

Read the first 1000 characters.

 if hasdata(fr)
    d = read(fr,1000,'SizeMethod','OutputSize','OutputType','char');
 end

Version History

Introduced in R2017b