readstruct
Description
creates a structure from a file with additional options specified by one or more name-value
pair arguments. For example, you can read the contents of the input file as XML when the
file extension in S
= readstruct(filename
,Name,Value
)filename
is not .xml
by calling
S = readstruct(filename,'FileType','xml')
.
Examples
Input Arguments
Output Arguments
Tips
Use XPath selectors to specify which elements of the XML input document to import. This table provides the XPath syntaxes that are supported by the XPath selector name-value argument
StructSelector
.Selection Operation Syntax Example Result Select the node whose name matches the node you want to select, regardless of its location in the document. Prefix the name with two forward slashes ( //
).data = readstruct("music.xml",StructSelector="//Ensemble")
data = struct with fields: Music: "Jazz" BandName: "Kool Katz" Instrumentation: [1×1 struct]
Select a specific node in a set of nodes. Provide the index of the node you want to select in square brackets ( []
).data = readstruct("music.xml",... StructSelector=... "//Ensemble/Instrumentation/Instrument[3]")
data = struct with fields: typeAttribute: "percussion" Text: "Drums" drumkit: ["Bass drum" "Floor tom" "Snare drum"... "Hi-hat" "Ride cymbal"]
Specify precedence of operations. Add parentheses around the expression you want to evaluate first. data = readstruct("students.xml",... StructSelector="//Student/Name[4]")
Error using readstruct No node with the selector '//Student/Name[4]' could be... found in the file 'students.xml'. 'StructSelector' must... refer to a valid node.
data = readstruct("students.xml",... StructSelector="(//Student/Name)[4]")
data = struct with fields: FirstNameAttribute: "Salim" LastNameAttribute: "Copeland"
Version History
Introduced in R2020b