Create a struct inside a cell array

5 ビュー (過去 30 日間)
Sebastian Holmqvist
Sebastian Holmqvist 2012 年 7 月 4 日
I'm using struct2xml from FileExhange and it's quite picky on the format of the structure to export. Basically I loose information when exporting data arrays (they are converted to strings). Example;
x = 1:3;
s.child.Text = x;
xml = struct2xml(s)
xml =
<?xml version="1.0" encoding="utf-8"?>
1 2 3
As you can see, the array of doubles has been converted into a string which makes it cumbersome for me to read the data back. I would like to have a format of;
<?xml version="1.0" encoding="utf-8"?>
1
2
3
but here's my pickle. In order for that to happen, I have to have a cell array of child-objects with a struct('Text', Value) in each.
s.child.Text = num2cell(x);
won't work, as struct2xml ultimately expects a struct (with field Text) but only gets a cell.
s.child = struct('Text', num2cell(x));
won't work, as that generates three separate structs. Multiple structs must be in cells.
cellfun(@struct, 'Text', num2cell(x));
won't accept the field name 'Text' unfortunately.
Any brilliant ideas?

採用された回答

Jan
Jan 2012 年 7 月 4 日
編集済み: Jan 2012 年 7 月 4 日
I do not know struct2xml, but the author does. Did you try to contact him already?
Intuitively I'd try:
s.child(1).Text = 1;
s.child(2).Text = 2;
s.child(3).Text = 3;
  4 件のコメント
Sebastian Holmqvist
Sebastian Holmqvist 2012 年 7 月 4 日
Your 3rd line won't give me the correct structure, but I appreciate the pre-allocation part. I guess I'll accept it for what it is. I was looking for some form of "vectorization" but it seems cumbersome.
Thanks!
Jan
Jan 2012 年 7 月 4 日
編集済み: Jan 2012 年 7 月 4 日
Are you sure? It looks like my code creates exactly the same s as yours. Anyway, vectorization is efficient, when the data can be represented in a vector like form. A loop is not necessarily slower, when you deal with nested structs and cells.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructured Data and XML Documents についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by