writestruct
Description
writestruct(
specifies options using one or more name-value arguments. For example, you can export the
contents of the input structure as an XML file regardless of the file extension specified in
S
,filename
,Name=Value
)filename
by calling
writestruct(filename,FileType="xml")
.
Examples
Write Structure to XML File
Create a structure from a text file that contains an XML structure, then write it to an XML file.
Display the contents of the music.txt
file.
type music.txt
<MusicalEnsemble> <Ensemble> <Music>Jazz</Music> <BandName>Kool Katz</BandName> <Instrumentation> <Instrument type="wind">Trumpet </Instrument> <Instrument type="percussion">Piano <pianotype>concert grand</pianotype> </Instrument> <Instrument type="percussion">Drums <drumkit>Bass drum</drumkit> <drumkit>Floor tom</drumkit> <drumkit>Snare drum</drumkit> <drumkit>Hi-hat</drumkit> <drumkit>Ride cymbal</drumkit> </Instrument> <Instrument type="string">Bass <basstype>upright</basstype> </Instrument> </Instrumentation> </Ensemble> <Musicians> <Name role="trumpeter">Miles</Name> <Name role="vocalist">Roger</Name> <Name role="pianist">Diana</Name> <Name role="drummer">George</Name> <Name role="bassist">John</Name> </Musicians> </MusicalEnsemble>
Import music.txt
as a structure. Specify the FileType
name-value argument as "xml"
to read the contents as an XML file.
S = readstruct("music.txt","FileType","xml")
S = struct with fields:
Ensemble: [1x1 struct]
Musicians: [1x1 struct]
Write the structure to an XML file. Display the contents of the XML file.
writestruct(S,"band.xml") type band.xml
<?xml version="1.0" encoding="UTF-8"?> <struct> <Ensemble> <Music>Jazz</Music> <BandName>Kool Katz</BandName> <Instrumentation> <Instrument type="wind"> <Text>Trumpet</Text> </Instrument> <Instrument type="percussion"> <Text>Piano</Text> <pianotype>concert grand</pianotype> </Instrument> <Instrument type="percussion"> <Text>Drums</Text> <drumkit>Bass drum</drumkit> <drumkit>Floor tom</drumkit> <drumkit>Snare drum</drumkit> <drumkit>Hi-hat</drumkit> <drumkit>Ride cymbal</drumkit> </Instrument> <Instrument type="string"> <Text>Bass</Text> <basstype>upright</basstype> </Instrument> </Instrumentation> </Ensemble> <Musicians> <Name role="trumpeter"> <Text>Miles</Text> </Name> <Name role="vocalist"> <Text>Roger</Text> </Name> <Name role="pianist"> <Text>Diana</Text> </Name> <Name role="drummer"> <Text>George</Text> </Name> <Name role="bassist"> <Text>John</Text> </Name> </Musicians> </struct>
Specify Root Node in Output File
Import the file music.xml
as a structure.
S = readstruct("music.xml")
S = struct with fields:
Ensemble: [1x1 struct]
Musicians: [1x1 struct]
Write the structure to an XML file. Name the root node JazzBand
. Display the contents of the XML file.
writestruct(S,"band.xml","StructNodeName","JazzBand") type band.xml
<?xml version="1.0" encoding="UTF-8"?> <JazzBand> <Ensemble> <Music>Jazz</Music> <BandName>Kool Katz</BandName> <Instrumentation> <Instrument type="wind"> <Text>Trumpet</Text> </Instrument> <Instrument type="percussion"> <Text>Piano</Text> <pianotype>concert grand</pianotype> </Instrument> <Instrument type="percussion"> <Text>Drums</Text> <drumkit>Bass drum</drumkit> <drumkit>Floor tom</drumkit> <drumkit>Snare drum</drumkit> <drumkit>Hi-hat</drumkit> <drumkit>Ride cymbal</drumkit> </Instrument> <Instrument type="string"> <Text>Bass</Text> <basstype>upright</basstype> </Instrument> </Instrumentation> </Ensemble> <Musicians> <Name role="trumpeter"> <Text>Miles</Text> </Name> <Name role="vocalist"> <Text>Roger</Text> </Name> <Name role="pianist"> <Text>Diana</Text> </Name> <Name role="drummer"> <Text>George</Text> </Name> <Name role="bassist"> <Text>John</Text> </Name> </Musicians> </JazzBand>
Specify Attribute Suffix
Specify the field names in an input structure to write as attributes in the output XML file.
Import the file music.xml
as a structure. 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 in the output structure that have associated attributes have the suffix "_att"
appended to the attribute names.
Query the field Musicians
to view its contents. Musicians
is a structure that contains five structures, each of which contains a field called Name
with an associated attribute called role
.
contents = S.Musicians
contents = struct with fields:
Name: [1x5 struct]
RolesNames = S.Musicians.Name
RolesNames=1×5 struct array with fields:
role_att
Text
Write the structure S
to an XML file named band.xml
and display its contents. The suffix "_att"
is appended to the attribute names in the file.
writestruct(S,"band.xml") type band.xml
<?xml version="1.0" encoding="UTF-8"?> <struct> <Ensemble> <Music>Jazz</Music> <BandName>Kool Katz</BandName> <Instrumentation> <Instrument> <type_att>wind</type_att> <Text>Trumpet</Text> </Instrument> <Instrument> <type_att>percussion</type_att> <Text>Piano</Text> <pianotype>concert grand</pianotype> </Instrument> <Instrument> <type_att>percussion</type_att> <Text>Drums</Text> <drumkit>Bass drum</drumkit> <drumkit>Floor tom</drumkit> <drumkit>Snare drum</drumkit> <drumkit>Hi-hat</drumkit> <drumkit>Ride cymbal</drumkit> </Instrument> <Instrument> <type_att>string</type_att> <Text>Bass</Text> <basstype>upright</basstype> </Instrument> </Instrumentation> </Ensemble> <Musicians> <Name> <role_att>trumpeter</role_att> <Text>Miles</Text> </Name> <Name> <role_att>vocalist</role_att> <Text>Roger</Text> </Name> <Name> <role_att>pianist</role_att> <Text>Diana</Text> </Name> <Name> <role_att>drummer</role_att> <Text>George</Text> </Name> <Name> <role_att>bassist</role_att> <Text>John</Text> </Name> </Musicians> </struct>
Write the structure S
to the XML file again, this time specifying the AttributeSuffix
name-value argument as "_att"
to indicate which field names in the input structure to write as attributes. Display the contents of band.xml
. The attributes in band.xml
do not have the suffix "_att"
.
writestruct(S,"band.xml","AttributeSuffix","_att") type band.xml
<?xml version="1.0" encoding="UTF-8"?> <struct> <Ensemble> <Music>Jazz</Music> <BandName>Kool Katz</BandName> <Instrumentation> <Instrument type="wind"> <Text>Trumpet</Text> </Instrument> <Instrument type="percussion"> <Text>Piano</Text> <pianotype>concert grand</pianotype> </Instrument> <Instrument type="percussion"> <Text>Drums</Text> <drumkit>Bass drum</drumkit> <drumkit>Floor tom</drumkit> <drumkit>Snare drum</drumkit> <drumkit>Hi-hat</drumkit> <drumkit>Ride cymbal</drumkit> </Instrument> <Instrument type="string"> <Text>Bass</Text> <basstype>upright</basstype> </Instrument> </Instrumentation> </Ensemble> <Musicians> <Name role="trumpeter"> <Text>Miles</Text> </Name> <Name role="vocalist"> <Text>Roger</Text> </Name> <Name role="pianist"> <Text>Diana</Text> </Name> <Name role="drummer"> <Text>George</Text> </Name> <Name role="bassist"> <Text>John</Text> </Name> </Musicians> </struct>
Replace NaN Values with Null Values
Display the contents of the musicians.txt
file.
type musicians.txt
{ "Orchestra": { "Music": "classical", "Instruments": [ { "Section": "string", "Musicians": 5 }, { "Section": "brass", "Musicians": 5 }, { "Section": "woodwind", "Musicians": NaN } ] }, "Band": [ { "Section": "guitar", "Musicians": 2 }, { "Section": "bass", "Musicians": NaN }, { "Section": "drums", "Musicians": 1 }, ] }
Create a structure from musicians.txt
. Parse the file as a JSON file.
S = readstruct("musicians.txt",FileType="json");
Write the structure S
to musicians.json
. Replace all NaN
values with null
values.
writestruct(S,"musicians.json",PreserveInfAndNaN=false) type musicians.json
{ "Orchestra": { "Music": "classical", "Instruments": [ { "Section": "string", "Musicians": 5.0 }, { "Section": "brass", "Musicians": 5.0 }, { "Section": "woodwind", "Musicians": null } ] }, "Band": [ { "Section": "guitar", "Musicians": 2.0 }, { "Section": "bass", "Musicians": null }, { "Section": "drums", "Musicians": 1.0 } ] }
Input Arguments
S
— Input structure
MATLAB® structure
Input structure, specified as a MATLAB structure. A structure is a data type that groups related data using data
containers called fields. Each field can contain any type of data. Access data in a
structure using dot notation of the form structName.fieldName
. For
more information on structures, see struct
.
When creating XML files, if a field in the input structure contains a missing value
or NaN
, writestruct
writes the contents of the
field as an empty string. When creating JSON files, writestruct
writes missing values as null
values and NaN
values as null
or NaN
values depending on the
PreserveInfAndNan
name-value argument.
filename
— Name of file to write
string scalar | character vector
Name of file to write, specified as a string scalar or character vector. If
filename
does not exist, then the writing function creates the
file. If filename
is the name of an existing file, then the writing
function overwrites it.
Depending on the location you are writing to, filename
can take
one of these forms.
Location | Form | ||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder | To write to the current folder, specify the name of the file in
Example:
Example:
| ||||||||
Other folders | To write to a folder that is not the current folder, specify the
full or relative pathname in
Example:
Example:
Example:
Example:
| ||||||||
Remote location | To write to a remote location, specify a uniform resource locator (URL) of the form:
Based on the remote location,
For more information, see Work with Remote Data. Example:
Example:
|
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: writestruct(S,"myfile.xml",StructNodeName="RootName")
specifies the name to use for the root node of the output XML file.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: writestruct(S,"myfile.xml","StructNodeName","RootName")
specifies the name to use for the root node of the output XML file.
FileType
— Type of file
"xml"
| "json"
Type of file, specified as one of these values:
"xml"
— Export the contents of the structure as an XML file, regardless of the file extension specified infilename
."json"
— Export the contents of the structure as a JSON file, regardless of the file extension specified infilename
. (since R2023b)
If you specify a file extension in filename
that is not
.xml
or .json
, you can specify
FileType
as "xml"
or "json"
to write the contents of the input structure as XML or JSON, respectively.
Example: FileType="xml"
PrettyPrint
— Indent text
true
or 1
(default) | false
or 0
Since R2023b
Indent text in the output file, specified as a numeric or logical
1
(true
) or 0
(false
). If you specify the value as true
,
then writestruct
writes the XML or JSON text with an indentation of
four spaces.
StructNodeName
— Root node name of output XML file
"struct"
(default) | string scalar | character vector
Root node name of the output XML file, specified as a string scalar or character
vector. If you do not specify StructNodeName
, the default name of
the root node is "struct"
.
Example: StructNodeName="RootName"
AttributeSuffix
— XML attribute suffix
"Attribute"
(default) | string scalar | character vector
XML attribute suffix, specified as a string scalar or character vector. This suffix indicates which field names in the input structure to write as attributes in the output XML file.
If the value of AttributeSuffix
matches the suffix of an
attribute name in the input structure, the suffix is dropped from the attribute name
in the output XML file. For example, if you specify AttributeSuffix
as "_att"
, a field in the input structure named
MyField_att
is written as an attribute named
MyField
in the XML file.
If you do not specify AttributeSuffix
,
writestruct
defaults to writing fields with the suffix
"Attribute"
as attributes in the output XML file.
Example: AttributeSuffix="_att"
PreserveInfAndNaN
— Preserve Inf
and NaN
values in JSON file
true
or 1
(default) | false
or 0
Since R2023b
Preserve Inf
and NaN
values in the output
JSON file, specified as a numeric or logical 1
(true
) or 0
(false
).
Specify this argument as false
to write all Inf
and NaN
values as JSON null
values.
Example: PreserveInfAndNaN=false
Version History
Introduced in R2020bR2023b: Write structures to JSON files
You can write the contents of MATLAB structure to JSON files.
R2023b: Control whether to indent text in output file
By default, writestruct
uses indentation of four spaces when writing
XML or JSON text. To write files without using indentation, specify the
PrettyPrint
name-value argument as false.
R2023b: Control whether to preserve Inf
and NaN
values in output JSON file
By default, writestruct
preserves Inf
and
NaN
values when writing to JSON files. To write Inf
and NaN
values as JSON null values, specify the
PreserveInfAndNaN
name-value argument as false.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)