writestruct
将结构体数组写入文件
说明
示例
基于包含 XML 结构体的文本文件创建一个结构体,然后再将该结构体写入一个 XML 文件。
显示 music.txt 文件内容。
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>
将 music.txt 作为结构体导入。将 FileType 名称-值参量指定为 ''xml",以将内容作为 XML 文件读取。
S = readstruct("music.txt","FileType","xml")
S = struct with fields:
Ensemble: [1×1 struct]
Musicians: [1×1 struct]
将结构体写入 XML 文件。显示 XML 文件的内容。
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>
将文件 music.xml 文件作为结构体导入。
S = readstruct("music.xml")S = struct with fields:
Ensemble: [1×1 struct]
Musicians: [1×1 struct]
将结构体写入 XML 文件。将根节点命名为 JazzBand。显示 XML 文件的内容。
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>
指定输入结构体中要作为属性写入输出 XML 文件的字段名称。
将文件 music.xml 文件作为结构体导入。将后缀 "_att" 追加到与输入 XML 文件中的属性对应的输出结构体的字段名称之后。
S = readstruct("music.xml","AttributeSuffix","_att")
S = struct with fields:
Ensemble: [1×1 struct]
Musicians: [1×1 struct]
输出结构体中具有关联属性的所有元素都在属性名称后追加后缀 "_att"。
查询字段 Musicians 以查看其内容。Musicians 是一个包含五个结构体的结构体,其中每个结构体都包含一个名为 Name 的字段以及一个名为 role 的关联属性。
contents = S.Musicians
contents = struct with fields:
Name: [1×5 struct]
RolesNames = S.Musicians.Name
RolesNames=1×5 struct array with fields:
role_att
Text
将结构体 S 写入名为 band.xml 的 XML 文件,并显示其内容。后缀 "_att" 追加到文件中的属性名称。
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>
再次将结构体 S 写入 XML 文件,这次将 AttributeSuffix 名称-值参量指定为 "_att",以指示输入结构体中的哪些字段名称要作为属性写入。显示 band.xml 的内容。band.xml 中的属性不再有后缀 "_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>
显示 musicians.txt 文件内容。
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
},
]
}
从 musicians.txt 创建一个结构体。将该文件解析为 JSON 文件。
S = readstruct("musicians.txt",FileType="json");
将结构体 S 写入 musicians.json。用 null 值替换所有 NaN 值。
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
}
]
}
输入参数
输入结构体,指定为 MATLAB 结构体。结构体是使用被称为字段的数据容器将相关数据组合在一起的一种数据类型。每个字段都可以包含任意类型的数据。使用 structName.fieldName 格式的圆点表示法来访问结构体中的数据。有关结构体的详细信息,请参阅 struct。
当创建 XML 文件时,如果输入结构体中的一个字段包含缺失值或 NaN,writestruct 将以空字符串形式写入该字段的内容。在创建 JSON 文件时,writestruct 将缺失值作为 null 值写入,将 NaN 值作为 null 或 NaN 值写入,具体取决于 PreserveInfAndNan 名称-值参量。
要写入的文件名,指定为字符串标量或字符向量。如果 filename 不存在,则写入函数会创建该文件。如果 filename 是现有文件的名称,则写入函数会覆盖它。
根据您写入的位置,filename 可以采用以下形式之一。
位置 | 形式 | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| 当前文件夹 | 要写入当前文件夹,请在 示例: 示例: | ||||||||
其他文件夹 | 要写入一个并非当前文件夹的文件夹,请在 示例: 示例: 示例: 示例: | ||||||||
远程位置 | 要写入远程位置,请指定以下形式的统一资源定位器 (URL):
根据远程位置,
有关详细信息,请参阅处理远程数据。 示例: 示例: |
名称-值参数
以 Name1=Value1,...,NameN=ValueN 的形式指定可选参量对组,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: writestruct(S,"myfile.xml",StructNodeName="RootName") 指定用于输出 XML 文件的根节点的名称。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。
示例: writestruct(S,"myfile.xml","StructNodeName","RootName") 指定用于输出 XML 文件的根节点的名称。
文件类型,指定为以下值之一:
"xml"- 将结构体的内容导出为 XML 文件,而不考虑filename中指定的文件扩展名。"json"- 将结构体的内容导出为 JSON 文件,而不考虑filename中指定的文件扩展名。 (自 R2023b 起)
如果您在 filename 中指定的文件扩展名不是 .xml 或 .json,您可以将 FileType 指定为 "xml" 或 "json" 以分别将输入结构体的内容作为 XML 或 JSON 写入。
示例: FileType="xml"
自 R2023b 起
在输出文件中缩进文本,指定为数值或逻辑值 1 (true) 或 0 (false)。如果您将该值指定为 true,则 writestruct 会写入缩进四个空格的 XML 或 JSON 文本。
输出 XML 文件的根节点名称,指定为字符串标量或字符向量。如果未指定 StructNodeName,则根节点的默认名称为 "struct"。
示例: StructNodeName="RootName"
XML 属性后缀,指定为字符串标量或字符向量。此后缀表示输入结构体中要作为属性写入输出 XML 文件的字段名称。
如果 AttributeSuffix 的值与输入结构体中属性名称的后缀匹配,则该后缀会从输出 XML 文件的属性名称中删除。例如,如果将 AttributeSuffix 指定为 "_att",则输入结构体中名为 MyField_att 的字段将作为 XML 文件中名为 MyField 的属性写入。
如果未指定 AttributeSuffix,writestruct 默认将后缀为 "Attribute" 的字段作为属性写入输出 XML 文件中。
示例: AttributeSuffix="_att"
自 R2023b 起
在输出 JSON 文件中保留 Inf 和 NaN 值,指定为数值或逻辑值 1 (true) 或 0 (false)。将此参量指定为 false 以将所有 Inf 和 NaN 值作为 JSON null 值写入。
示例: PreserveInfAndNaN=false
版本历史记录
在 R2020b 中推出您可以将 MATLAB 结构体中的内容写入 JSON 文件。
默认情况下,writestruct 在写入 XML 或 JSON 文本时使用四个空格的缩进。要在写入文件时不使用缩进,请将 PrettyPrint 名称-值参量指定为 false。
默认情况下,在写入 JSON 文件时,writestruct 会保留 Inf 和 NaN 值。要将 Inf 和 NaN 值作为 JSON 空值写入,请将 PreserveInfAndNaN 名称-值参量指定为 false。
另请参阅
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)