matlab.io.datastore.DsFileReader.seek - Seek to a position in the file - MATLAB (original) (raw)
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](#d126e447718),[n](#d126e447750))
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](#d126e447718),[n](#d126e447750),[Name,Value](#namevaluepairarguments))
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
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 backward in the file.
Example: seek(fr,20)
Data Types: double
Name-Value Arguments
Specify optional pairs of arguments asName1=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 bytrue
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 theseek
operation from the current position indicator in the file.'start-of-file'
- Starts theseek
operation from position zero.'end-of-file'
- Starts theseek
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 forairlinesmall.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 by299
bytes. To get past the variable names line, use theseek
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