Write struct to table in excel

5 views (last 30 days)
Mini Me
Mini Me on 18 Jun 2020
I have a very embedded struct that I would like to turn into an excel table. my struct size is 1 X 1 but it has alot of attributes and children and data in it. Unfortunately when I use writetable(struct2table(mlStruct,'AsArray',true), 'file_name.xlsx') I only get the name of the structure. My goal is to get all the data in an excel sheet. I have attached the structure with this post, can anyone help me with this.
I use the parseXML.m file to parse my xml data into struct and my plan is to convert it to excel. The xml file is too big to import into excel that is why I am choosing to do it this way. see code below
function theStruct = parseXML(filename)
% PARSEXML Convert XML file to a MATLAB structure.
tree = xmlread(filename);
error('Failed to read XML file %s.',filename);
% Recurse over child nodes. This could run into problems
% with very deeply nested trees.
theStruct = parseChildNodes(tree);
error('Unable to parse XML file %s.',filename);
% ----- Local function PARSECHILDNODES -----
function children = parseChildNodes(theNode)
% Recurse over node children.
children = [];
if theNode.hasChildNodes
childNodes = theNode.getChildNodes;
numChildNodes = childNodes.getLength;
allocCell = cell(1, numChildNodes);
children = struct( ...
'Name', allocCell, 'Attributes', allocCell, ...
'Data', allocCell, 'Children', allocCell);
for count = 1:numChildNodes
theChild = childNodes.item(count-1);
children(count) = makeStructFromNode(theChild);
% ----- Local function MAKESTRUCTFROMNODE -----
function nodeStruct = makeStructFromNode(theNode)
% Create structure of node info.
nodeStruct = struct( ...
'Name', char(theNode.getNodeName), ...
'Attributes', parseAttributes(theNode), ...
'Data', '', ...
'Children', parseChildNodes(theNode));
if any(strcmp(methods(theNode), 'getData'))
nodeStruct.Data = char(theNode.getData);
nodeStruct.Data = '';
% ----- Local function PARSEATTRIBUTES -----
function attributes = parseAttributes(theNode)
% Create attributes structure.
attributes = [];
if theNode.hasAttributes
theAttributes = theNode.getAttributes;
numAttributes = theAttributes.getLength;
allocCell = cell(1, numAttributes);
attributes = struct('Name', allocCell, 'Value', ...
for count = 1:numAttributes
attrib = theAttributes.item(count-1);
attributes(count).Name = char(attrib.getName);
attributes(count).Value = char(attrib.getValue);
and I call it here:
sampleXMLfile = 'filename.xml';
mlStruct = parseXML(sampleXMLfile);
writetable(struct2table(mlStruct(:)), 'file_name.xlsx')
Juan Miguel Serrano Rodríguez
This seems like the right approach, maybe when having a table variable with nested tables and using the function writetable, different sheets within the spreadsheet should be created automatically containing the contents of the different sub-tables and when using readtable they should get automatically imported into the original nested table variable.

Sign in to comment.

Answers (0)


Find more on Data Import from MATLAB in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!