readstruct

Reads an XML file into a scalar struct.

Category: Data Import and Analysis > Data Import and Export

Syntax

S = readstruct(filename)

Parses the file and returns a struct with one field per element.

Description

Each child element of the root becomes a field named after the tag. Text that looks like a number is converted to a double, other text is returned as a string, and elements with the same name collapse into one array-valued field. Nested elements give nested structs.

Input arguments

filename
Name of the file. XML is the format covered here.

Output arguments

S
Scalar struct. Text values become strings, numeric text becomes numbers, and repeated elements become arrays.

Example

Read an XML file

fid = fopen('p.xml', 'w');
fprintf(fid, '<p><name>Ann</name><age>30</age><tag>a</tag><tag>b</tag></p>');
fclose(fid);
S = readstruct('p.xml')
S.age + 1
S.tag
Output
S = struct with fields:

    name: "Ann"
     age: 30
     tag: ["a"    "b"]
ans = 31
ans = 1×2 string array

    "a"    "b"

See also

readtable jsondecode fileread struct