Main Content

writestruct

Write structure to file

    Description

    example

    writestruct(S,filename)writes a structure to a file with the name and extension specified byfilename. Thewritestructfunction automatically writes the input structure to an XML file when.xmlis specified as the file extension infilename.

    example

    writestruct(S,filename,Name,Value)writes a structure to a file with additional options specified by one or more name-value pair arguments. For example, you can export the contents of the input structure as an XML file regardless of the file extension specified infilenameby callingwritestruct(filename,"FileType","xml").

    Examples

    collapse all

    Create a structure from a text file that contains an XML structure, then write it to an XML file.

    The filemusic.txthas the following structure.

    Read the text filemusic.txtas a structureS. Specify'FileType'as'xml'to read the contents of the input as an XML file.

    S = readstruct("music.txt","FileType","xml")
    S =struct with fields:Ensemble: [1x1 struct] Musicians: [1x1 struct]

    WriteSto the XML file namedband.xml. Display the contents ofband.xml.

    writestruct(S,"band.xml") typeband.xml
       Jazz Kool Katz   Trumpet   Piano   Drums   Bass  concert grand Bass drum Floor tom Snare drum Hi-hat Ride cymbal upright     Miles   Roger   Diana   George   John   

    Read the filemusic.xmlas a structureS.

    S = readstruct("music.xml")
    S =struct with fields:Ensemble: [1x1 struct] Musicians: [1x1 struct]

    WriteSto the XML file namedband.xml. Name the root nodeJazzBand. Display the contents ofband.xml.

    writestruct(S,"band.xml","StructNodeName","JazzBand") typeband.xml
       Jazz Kool Katz   Trumpet   Piano concert grand   Drums Bass drum Floor tom Snare drum Hi-hat Ride cymbal   Bass upright      Miles   Roger   Diana   George   John   

    Specify the field names in the input structure to write as attributes in the output XML file.

    Read the filemusic.xmlas a structureS. Append the suffix"_att"to the field names of the output structure that correspond to attributes in the input XML file.

    S = readstruct("music.xml","AttributeSuffix","_att")
    S =struct with fields:Ensemble: [1x1 struct] Musicians: [1x1 struct]

    All elements inSthat have associated attributes will have the suffix"_att"appended to the attribute names.

    Query the fieldMusiciansto view its contents.Musiciansis a structure that contains five structures, each of which contain a field calledNamewith an associated attribute calledrole.

    S.Musicians.Name
    ans=1×5 struct array with fields:role_att Text

    WriteSto the XML file namedband.xmland display its contents. The suffix"_att"被附加到fi属性名称le.

    writestruct(S,"band.xml") typeband.xml
       Jazz Kool Katz   wind Trumpet   percussion Piano concert grand   percussion Drums Bass drum Floor tom Snare drum Hi-hat Ride cymbal   string Bass upright      trumpeter Miles   vocalist Roger   pianist Diana   drummer George   bassist John   
    writestruct(S,"band.xml","AttributeSuffix","_att")

    Write the structureSto the XML file again, this time specifying the value of"AttributeSuffix"as"_att"to indicate which field names in the input structure to write as attributes. Display the contents ofband.xml. The attributes inband.xmldo not have the suffix"_att".

    typeband.xml
       Jazz Kool Katz   Trumpet   Piano concert grand   Drums Bass drum Floor tom Snare drum Hi-hat Ride cymbal   Bass upright      Miles   Roger   Diana   George   John   

    Input Arguments

    collapse all

    Input structure, specified as a MATLAB structure. A structure is a data type that groups related data using data containers called fields.

    If a field in the input structure contains a missing value orNaN,writestruct会写th的内容e field as an empty string.

    For more information on structures, seestruct.

    File name to write to, specified as a character vector or string scalar. Iffilenamedoes not exist, then the writing function creates the file. Iffilenameis the name of an existing file, then the writing function overwrites it.

    Depending on the location you are writing to,filenamecan take on one of these forms.

    Location

    Form

    Current folder

    To write to the current folder, specify the name of the file infilename.

    Example:'myFile.xml'

    Other folders

    To write to a folder different from the current folder, specify the full or relative path name infilename.

    Example:'C:\myFolder\myFile.xml'

    Example:'myFolder\myFile.xml'

    Remote Location

    To write to a remote location, specify a uniform resource locator (URL) of the form:

    scheme_name://path_to_file/my_file.ext

    Based on your remote location,scheme_namecan be one of the values in this table.

    Remote Location scheme_name
    Amazon S3™ s3
    Windows Azure®Blob Storage wasb,wasbs
    HDFS™ hdfs

    For more information, seeWork with Remote Data.

    Example:s3: / / bucketname / path_to_file / myFile.xml '

    Name-Value Pair Arguments

    Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

    Example:'StructNodeName','RootName'specifies the name to use for the root node of the output XML file.

    Type of file, specified as the comma-separated pair consisting of'FileType'and one of these values:

    • 'auto'— Automatically detect the file format to write from the extension specified infilename.

    • 'xml'— Export the contents of the structure as an XML file, regardless of the file extension specified infilename.

    If you specify a file extension infilenamethat is not.xml, you can specify the value of'FileType'as'xml'to write the contents of the input structure as XML.

    Example:'FileType','xml'

    Root node name of output XML file, specified as the comma-separated pair consisting of'StructNodeName'and either a character vector or string scalar containing the name of the root node to be used in the output. If you do not specifyStructNodeName, the default name of the root node is"struct".

    Example:'StructNodeName','RootName'

    Attribute suffix, specified as the comma-separated pair consisting of'AttributeSuffix'and either a character vector or string scalar indicating which field names in the input structure to write as attributes in the output XML file.

    For example, for a field nameAttName_attin the input structure, you can specify'AttributeSuffix','_att'to indicate that'AttName'should be written out as an attribute in the output XML file.

    • If you do not specify'AttributeSuffix',writestructdefaults to writing fields with the suffix'Attribute'as attributes in the output XML file.

    • If the attribute specified as the value of'AttributeSuffix'matches the suffix appended to the attribute name in the input structure, the suffix will be dropped from the attribute name in the output XML file. For example, if you specify'AttributeSuffix','_att', a field in the input structure namedMyField_attwill correspond to the attribute namedMyFieldin the XML file.

    Example:'AttributeSuffix','_att'

    See Also

    Introduced in R2020b