Clear Filters
Clear Filters

Write struct to table in excel

20 views (last 30 days)
Mini Me
Mini Me on 18 Jun 2020
Hello,
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.
try
tree = xmlread(filename);
catch
error('Failed to read XML file %s.',filename);
end
% Recurse over child nodes. This could run into problems
% with very deeply nested trees.
try
theStruct = parseChildNodes(tree);
catch
error('Unable to parse XML file %s.',filename);
end
% ----- 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);
end
end
% ----- 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);
else
nodeStruct.Data = '';
end
% ----- 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', ...
allocCell);
for count = 1:numAttributes
attrib = theAttributes.item(count-1);
attributes(count).Name = char(attrib.getName);
attributes(count).Value = char(attrib.getValue);
end
end
and I call it here:
sampleXMLfile = 'filename.xml';
mlStruct = parseXML(sampleXMLfile);
writetable(struct2table(mlStruct(:)), 'file_name.xlsx')
  2 Comments
Eric Sofen
Eric Sofen on 25 Jun 2020
The challenge with writing all the data in mlStruct into an Excel spreadsheet is that there's a lot more data nested in structs within the fields of mlStruct. struct2table and writetable won't flatten all those layers of nesting out for you (although improving the tools to work with nested structs is something that we're looking at).
Within mlStruct, both mlStruct.Attributes and mlStruct.Children contain struct arrays with different fields. Therefore, I don't think your data can be flattened into a single, simple tabular form. I think you'll want to break up the top level fields in mlStruct into separate tables (i.e. an Attributes table and a Children table). Then each of those could be written to separate pages in an Excel spreadsheet.
Hope that helps to point you in a direction to solve your problem.
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)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!