Write struct to table in excel

5 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
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)

Categories

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

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!