Main Content


Write case names to file



casewrite(strmat,filename) writes the contents of the character array or string column vector strmat to a file filename. Each row of strmat represents one case name, and casewrite writes each name to a separate line in filename. Specify filename as either a file name (to write the file to the current folder) or a complete path name (to write the file to a different folder).

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

casewrite(strmat) opens the Select File to Write dialog box so that you can interactively specify the file to write.


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

Case names, specified as a character array or string column vector. Each row of strmat corresponds to a case name and becomes a line in filename.

Data Types: char | string

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

Depending on the location you are writing to, filename has one of these forms.

Location of FileForm
Current folder

Specify the name of the file in filename.

Example: 'myTextFile.csv'

Folder that is different from the current folder

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'     }

Version History

Introduced before R2006a