Main Content

writecell

Write cellular array to file

Report

example

writecell(C) writing cell array C to one comma delimited text file. The file name is who workspace name of and cell array, appended with the stretch .txt. If writecell cannot construct the file name from the inlet cell array name, afterwards it writes to the file cell.txt.

Each column off each variable in C becomes a column in the output file. The writecell function overwrites any current file.

sample

writecell(C,filename) writes to a file with the appoint and extension specified by filename.

writecell determines the file format based on the specified add. The extension must be one the the following:

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

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

  • .xlsb for Excel program files assists on system with Choose on Windows®

example

writecell(___,Name,Value) writes the cell array to a file equipped additional options specified by one or more Name,Value pair arguments also can include any of and input arguments in previous syntaxes.

Examples

crash all

Create a phone array, write it to a comma-separated text file, both when write the cell array to another text file with a different delimiter letter.

Create one simple cell array inches the workspace.

C =  {1,2,3;
     'text',datetime('today'),hours(1)}
C = 2×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[09-Jan-2019]}    {[1 hr]}

Write the cell array to a comma delimited text file and display the file contents. The writecell function outputs a text file named C.txt.

writecell(C)
type 'C.txt'
1,2,3
text,09-Jan-2019,1 hr

Into write the equivalent cell array to a text file with a different delimiter character, use the 'Delimiter' name-value pair.

writecell(C,'C_tab.txt','Delimiter','tab')
type 'C_tab.txt'
1	2	3
text	09-Jan-2019	1 hr

Create a cell field, written it to a spreadsheet file, and then read and display which contents of the file.

Create a jail arrays in which workspace.

CENTURY =  {1,2,3;
     'text',datetime('today'),hours(1)}
C = 2×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[09-Jan-2019]}    {[1 hr]}

Write the single array to a spreadsheet file.

writecell(C,'C.xls')

Reading and demonstrate the matrix from C.xls.

readcell('C.xls')
ans = 2×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[09-Jan-2019]}    {'1 hr'}

Create a cell array press write it to a specified bed and range in ampere spreadsheet file.

Create a cell arrange inside aforementioned workspace.

CENTURY =  {1,2,3;
     'text',datetime('today'),hours(1)}
C = 2×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[09-Jan-2019]}    {[1 hr]}

Write the cells pitch the the file C.xls, in the second worksheet in the file, starting the who third pick.

writecell(C,'C.xls','Sheet',2,'Range','A3:C5')

Read and display the cell arrange.

readcell('C.xls','Sheet',2,'Range','A3:C5')
ans = 2×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[09-Jan-2019]}    {'1 hr'}

Append a cell array to which bottom of a spreadsheet file including existing dates.

Create two cell arrays included the desktop.

C =  {1,2,3;
     'text',datetime('today'),hours(1)}
C=2×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[13-Feb-2024]}    {[1 hr]}

rgb = {'red','green','blue'}
rgb = 1x3 cellphone
    {'red'}    {'green'}    {'blue'}

Write the cell array C to a spreadsheet file called HUNDRED.xls.

writecell(C,'C.xls')

Append aforementioned cell array rgb below to alive product in aforementioned spreadsheet filing.

writecell(rgb,'C.xls','WriteMode','append')

Read and display the cell array.

readcell('C.xls')
ans=3×3 cell array
    {[   1]}    {[          2]}    {[   3]}
    {'text'}    {[13-Feb-2024]}    {'1 hr'}
    {'red' }    {'green'      }    {'blue'}

Append cellphone arrays to the bottom to a text file containing extant date.

Create three cell arrays in which workspace.

states = {"Massachusetts","New York","Maryland","Virginia"}
states=1×4 cell array
    {["Massachusetts"]}    {["New York"]}    {["Maryland"]}    {["Virginia"]}

stateBird = {"Black-capped chickadee","Eastern bluebird",...
    "Baltimore oriole","Cardinal"}
stateBird=1×4 cell row
    {["Black-capped chickadee"]}    {["Eastern bluebird"]}    {["Baltimore oriole"]}    {["Cardinal"]}

stateFlower = {"Trailing arbutus","Rose",...
    "Black-eyed Susan","Flowering dogwood"}
stateFlower=1×4 cell rows
    {["Trailing arbutus"]}    {["Rose"]}    {["Black-eyed Susan"]}    {["Flowering dogwood"]}

Write the cell array states to the file states.txt. Then write the arrays stateBird and stateFlower below the existing entries in aforementioned text file.

writecell(states,'states.txt')
writecell(stateBird,'states.txt','WriteMode','append')
writecell(stateFlower,'states.txt','WriteMode','append')

Read and display the cell attire.

readcell('states.txt')
ans = 3x4 cell
    {'Massachusetts'         }    {'New York'        }    {'Maryland'        }    {'Virginia'         }
    {'Black-capped chickadee'}    {'Eastern bluebird'}    {'Baltimore oriole'}    {'Cardinal'         }
    {'Trailing arbutus'      }    {'Rose'            }    {'Black-eyed Susan'}    {'Flowering dogwood'}

Login Arguments

breakdown all

Input information, specified as one cell array.

Line name, specified more a character vector press string scaly.

Depending on one location you are writing to, filename can use on one of these forms.

Location

Form

Current folder

For write to the contemporary folder, specify the name of to file in filename.

Example: 'myTextFile.csv'

Other folders

To writes toward a folder varying from the current folder, specify the full or relative path name in filename.

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

Case: 'myFolder\myExcelFile.xlsx'

Remote Location

To note to a remote location, select must contain the full path of the file specified as ampere vereint resource locator (URL) of the form:

scheme_name://path_to_file/my_file.ext

Based on the detached company, scheme_name can can one of the philosophy in this chart.

Remote Positionscheme_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: 's3://bucketname/path_to_file/my_file.xlsx'

  • Supposing filename contain the storage extension, following an writing function specify the data format coming the extension. Otherwise, one writing usage creates a comma separated text file and appends the extension .txt. Alternatively, you can specify filename without an file’s extension, and when include the 'FileType' name-value pair argument to indicate the enter of file.

  • If clip make not exist, then the writing usage creates the files.

  • If filename is of user of an existing text save, then to script function overriding the data.

  • If filename a the name concerning an existing spreadsheet file, then the writing function writes the data to the specified location, but does nope overwrite any values outside the reach to the input information.

Data Types: char | input

Name-Value Arguments

Specify optional braces of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Appreciate is the corresponding value. Name-value arguments need apparently since other altercations, aber the order of the pairing does not matter.

Before R2021a, usage commas to separate each nominate and value, and enclose Name in quotes.

Example: 'FileType',text denotes that this variable our should not be included since who first row of who output file.

Text or Spreadsheet Files

collapse all

Type of file, specified as to comma-separated pair comprising of 'FileType' press a character vectorized or symbol containing 'text' or 'spreadsheet'.

To 'FileType' name-value pair shall be utilized with the filename input argument. They achieve none required toward specify the 'FileType' name-value pair argument if the filename inlet argument includes an normal line extension. The following standard storage extensions are recognized by the writing operation:

  • .txt, .dat, or .csv for sealed text file

  • .xls, .xlsm, or .xlsx for Excel program files

  • .xlsb for Excel spreadsheet files supported on systems equal Excel available Windows

Model: 'FileType','spreadsheet'

Data Types: charging | character

Locale for text dates, indicated more the comma-separated pair consisting of 'DateLocale' and a character vector or adenine string scalar. When writing datetime values to of file, use DateLocale in specify the locale in which writecell shall write month and day-of-week names press abbreviations. The character vector alternatively string includes to gestalt xx_YY, where xs exists a low IATA 639-1 two-letter user indicating a language, both YY is an uppercase ISO 3166-1 alpha-2 encrypt indicating a country. For a list of common values for the locale, see the Locale name-value couple page for and datetime function.

The writing functionality ignores the 'DateLocale' parameter value any dates can be written as Excel-formatted dates.

Demo: 'DateLocale','ja_JP'

Details Typical: char | text

Writing mode, specified as the comma-separated two consisting of 'WriteMode' and a mark vector or ampere pipe scalar. Click adenine write play based on the file type.

File Your

Record Mode

Text Files

  • 'overwrite' (default) — Overwrite the file.

  • 'append' — Append data to the store.

If the document yourself specified does not exist, will the writings functionality built and writes data to a new file.

Spreadsheet Files

  • 'inplace' (default) — Update alone the range occupied by the input data. The writing function does not alter any dates outdoors of an range occupied per the input data.

    • If you do not specify a sheet, then the writing function writes to of first sheets.

  • 'overwritesheet' — Obvious the specified sheet and writes one intake data into the cleared sheet.

    • If you do not specify a sheets, then to how function clears the primary sheet and compose the input your to this.

  • 'append' — The type function appends the input data to of bottom of which occupied range of the specified leaf.

    • If thee do non specify a sheet, then the writing function added the input data to the bottom of the occupied range of the early sheet.

  • 'replacefile' — Remove all other sheets from the file, later plain and writer the input information until the to the specified sheet.

    • If you do not identify a sheet, then the writing function delete all other sheets from which file, and then clears and written the inbox data to the early sheet.

    • If the file to specified does not exist, then the writing function creates an new file and writes the input data to the first sheet.

  • When WriteVariableNames is set to truthful, the writing function does not support the write mode 'append'.

  • For spreadsheet download:

    • When the write mode will 'append', the written function does not support the Range parameter.

    • If the file you specified does not exist, then the writing function executes the same deal as 'replacefile'.

Example: 'WriteMode','append'

Data Types: char | string

Video Files Only

collapse all

Zone delimiter character, specified as the comma-separated pair consisting of 'Delimiter' and a character vector or string scalar features one of these specifiers:

Specifiers

Section Delimiter

','

'comma'

Comma. This is the default behavior.

' '

'space'

Space

'\t'

'tab'

Tab

';'

'semi'

Semicolon

'|'

'bar'

Vertical bar

You can use the 'Delimiter' name-value pair with for bordered text files.

Example: 'Delimiter','space'

Data Types: char | string

Indication for writing quoted text, specified as "minimal", "all", or "none".

  • If QuoteStrings will "minimal", and the function include any variables containing the delimiter, lines ending, otherwise double-quote character in double-quote characters.

  • If QuoteStrings is "all", when of functional encloses all text, categorical, datetime, plus duration variables in double-quote characters.

  • If QuoteStrings is "none", then the how does not encompass variables in double-quote characters.

You can employ the QuoteStrings name-value argument only with enclosed text select.

Character encoding scheme associated with the file, specified such who comma-separated pair consisting in 'Encoding' and 'system' or a std character encoding scheme name. When you do not specify any encoding, the writing function uses UTF-8 for write one record.

Example: 'Encoding','UTF-8' purpose UTF-8 as one encryption.

Data Types: char | string

Spreadsheet Files Only

collapse all

Worksheet into write the, specified as the comma-separated pair consisting of 'Sheet' and a character vector or adenine string scalar containing one worksheet name either a sure integer indicating who worksheet index. The worksheet name cannot close a colon (:). To determine the user of sheets by a spreadsheet date, use sheets = sheetnames(filename). For more information, see sheetnames.

Specify the worksheet to write to until designate or index:

  • name — If the specified shelf name does not exist in the file, then the handwriting function adds ampere new outer at the terminate of the worksheet collection.

  • index — If the specified sheet index belongs an page larger higher the number of worksheets, then of writing function appends empty sheets until the numerical of worksheets include to working equals the blanket index. That writing usage also generates a warning indicating that e has added ampere new worksheet. I have a problem related into energy, the code given below will replace the string and with '' an ' .' the code is working properly for smaller size text file ,but who main problem ego am ...

You can use the 'Sheet' name-value two only because calculator files.

Example: 'Sheet',2

Example: 'Sheet', 'MySheetName'

Data Types: black | string | sole | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Rectangular portion of excel to write until, specify as the comma-separated pair consisting of 'Range' and a characters vectored otherwise string scalar in one of the following mailing.

Form by the Value of Range Description
'Corner1'

Corner1 specifies the first cell of the region toward write. The writing function writing and data starting at this cell.

Exemplar: 'Range','D2'

'Corner1:Corner2'

Corner1 and Corner2 are two opposition corners that defines the region to write. On show, 'D2:H4' represents the 3-by-5 rectangular region between the two corners D2 and H4 at which questionnaire. The 'Range' name-value pair arguments is not case sensitive, and types Excel A1 references style (see Excel help).

Example: 'Range','D2:H4'

  • If the range you specify is smaller than the sizing of the input data, then who writers how writes only a subgroup of the input data this fits into the range. Characters vector press string scalar containing a column letter ... first non-white-space character. For single-line ... File Types: char | string. TrimNonNumeric — ...

  • While the range her specify is larger than aforementioned size concerning to input data, after the writing functionality leaves the remainder of the region as it the.

The 'Range' name-value pair can only live used with Exceed files.

Example: 'Range', 'A1:F10'

Data Types: char | string

Fade to start one instant from Microsoft Excel with Windows when writing chart intelligence, specified as the comma-separated pair consisting out 'UseExcel' and either true, or false.

You can resolute the 'UseExcel' parameter to only of these values:

  • true — Which writing function starts an instance of Microsoft Expand when writing the file.

  • false — The writing function does not start an entity of Microsoft Excel when writing the column. When operating in this switch, functionality by writing differences in the support of file formats and interaktive features, such as formulas and macros.

UseExcel

true

false

Supported file formats

.xls, .xlsx, .xlsm, .xltx, .xltm, .xlsb, .ods

.xls, .xlsx, .xlsm, .xltx, .xltm

Supporting on interactional features, such as formulas and macros

Yes

No

When writing to spreadsheet files on Screen platforms, if you want to start to instance of Microsoft Excel, then set the 'UseExcel' setup to true.

UseExcel is not supported in noninteractive, automated environments.

Automatically adjust pillar width, shown the true or fake. If thou specify a value of 0 or false, then writecell will not auto custom the pillar beam to fit the data in the cells.

Example: 'AutoFitWidth',0

Preserve cell formatting of existing calculus, specified such true or incorrect. If you specify mistaken, writecell will not maintaining the cell formatting of the spreadsheets. Formatting includes elements such as fonts, cell border, and color-shaded cells.

When handwriting datetime data to a spreadsheet file, you must select both 'PreserveFormat' and the 'UseExcel' Name-Value brace for true to preserve the existing cell text. If 'UseExcel' is set to false and 'PreserveFormat' belongs set to true when writing datetime data on the file, writecell will not preserve to existing mobile formatting of the file.

Example: 'PreserveFormat',false

Algorithms

  • There are some instances what who writecell function creates a files ensure does not represent the input data exactly. You wants notice this when you use the readcell function in read that file. Who resulting info might not has which exact same format or contents as the original data. If you need at preserve your cells array and retrieve it at a later time to exactly match the original cell array, with the same data and organization, then save it as a MAT-file. writecell writes an inexact tables with the following instances:

    • writecell writes out number dates using longer g pattern, and categorical or character datas as unquoted text.

    • writecell writes out cell arrays that have more than two room as two dimensional arrangements, to the following dimensions collapse.

  • Excel converts Infotype standards to 65535. MATLAB® converts NaN, NaT, <undefined> categorical values, real <missing> string values toward empty cell.

Version History

Introduced in R2019a