Main Content

previousfile

Information on previous file in file-set

Since R2020a

Description

info = previousfile(fs) returns the information on the previous file in the FileSet object fs, then recedes the internal pointer to the previous file. Subsequent calls to the previousfile function continue reading from the endpoint of the previous call.

example

Examples

collapse all

Return to a previous file in a file-set to get file information.

Create a file-set fs for a collection of files.

folder = {'accidents.mat','airlineResults.mat','census.mat','earth.mat'}
folder = 1x4 cell
    {'accidents.mat'}    {'airlineResults.mat'}    {'census.mat'}    {'earth.mat'}

fs = matlab.io.datastore.FileSet(folder)
fs = 
  FileSet with properties:

                    NumFiles: 4
                NumFilesRead: 0
                    FileInfo: FileInfo for all 4 files
    AlternateFileSystemRoots: {}

Get the information for the first file and move the internal pointer forward one file.

file1 = nextfile(fs)
file1 = 
  1x1 FileInfo
                                                        Filename                                                         FileSize
    _________________________________________________________________________________________________________________    ________

    "/mathworks/devel/bat/filer/batfs2566-0/Bdoc24b.2725827/build/runnable/matlab/toolbox/matlab/demos/accidents.mat"      7343  


Get information for the second file and move the internal pointer forward one file.

file2 = nextfile(fs)
file2 = 
  1x1 FileInfo
                                       Filename                                        FileSize 
    ______________________________________________________________________________    __________

    "/tmp/Bdoc24b_2725827_1481070/tp890d7354/matlab-ex34890761/airlineResults.mat"    1.5042e+05


Get information for the second file again and recede the internal pointer to the second file.

prev = previousfile(fs)
prev = 
  1x1 FileInfo
                                       Filename                                        FileSize 
    ______________________________________________________________________________    __________

    "/tmp/Bdoc24b_2725827_1481070/tp890d7354/matlab-ex34890761/airlineResults.mat"    1.5042e+05


Input Arguments

collapse all

File-set, specified as a FileSet object. To create a FileSet object, see matlab.io.datastore.FileSet.

Output Arguments

collapse all

This property is read-only.

Information about files in the FileSet object, returned as a FileInfo object with the following properties:

  • Filename — Name of the file in the FileSet object. The name contains the full path of the file.

  • FileSize — Size of the file in number of bytes.

Tips

  • The previousfile function returns an error if no files have been read from the FileSet object. To avoid this error, use previousfile along with hasPreviousFile and reset. The hasPreviousFile method checks if a file still remains in the Fileset object, while the reset method resets the internal pointer to the beginning of the Fileset object.

Version History

Introduced in R2020a