Read case names from file



names = caseread(filename) reads the contents of filename and returns a character array names. The caseread function treats each line of the file as a separate case name. Specify filename as either the name of a file in the current folder or the complete path name of a file.

filename can have one of the following file extensions:

  • .txt, .dat, or .csv for delimited text files

  • .xls, .xlsm, or .xlsx for Excel® spreadsheet files

names = caseread opens the Select File to Open dialog box so that you can interactively select the file to read.


collapse all

Create a character array of case names representing months.

months = char('January','February', ...

Write the names to a file named months.dat. View the contents of the file by using the type function.

type months.dat

Read the names in the months.dat file.

names = caseread('months.dat')
names = 5x8 char array
    'January '
    'March   '
    'April   '
    'May     '

Input Arguments

collapse all

Name of the file to read, specified as a character vector or string scalar.

Depending on the location of the file, filename has one of these forms.

Location of FileForm
Current folder or folder on the MATLAB® path

Specify the name of the file in filename.

Example: 'myTextFile.csv'

Folder that is not the current folder or a folder on the MATLAB path

Specify the full or relative path name in filename.

Example: 'C:\myFolder\myTextFile.csv'

Example: 'months.dat'

Data Types: char | string

Alternative Functionality

Instead of using casewrite and caseread with character arrays, consider using writecell and readcell with cell arrays. For example:

months = {'January';'February';'March';'April';'May'};
names = readcell('months.dat')
names =

  5×1 cell array

    {'January' }
    {'March'   }
    {'April'   }
    {'May'     }

Introduced before R2006a