how to save Struct to txt file

266 ビュー (過去 30 日間)
Mr. Unkown
Mr. Unkown 2015 年 4 月 16 日
回答済み: John Taseff 2023 年 7 月 11 日
hello,
I have my data saved in struct format like this
===
MyData=
1x405 struct array with fields
Distance
Binary
Angle
===
my question is: how to save these cell arrays to text files please?

回答 (4 件)

Guillaume
Guillaume 2015 年 4 月 16 日
Assuming the fields are scalar, the simplest way is to convert the structure to a table and then use writetable:
writetable(struct2table(MyData), 'somefile.txt')
  2 件のコメント
Mr. Unkown
Mr. Unkown 2015 年 4 月 16 日
Thanks Guillaume, I have tried this and got undefined function or variable 'struct2table'
Guillaume
Guillaume 2015 年 4 月 17 日
You must have an old version of matlab then. struct2table has existed since R2013b.

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


John Taseff
John Taseff 2023 年 7 月 11 日
google brought me here, updating for future denizens - as of R2020b you can use writestruct

Adam
Adam 2015 年 4 月 16 日
編集済み: Adam 2015 年 4 月 16 日
doc fprintf
You will have to put the code together yourself to use fprintf as there is no builtin function that can magically save an arbitrary struct to a text file.
I assume you want to use the file outside of Matlab otherwise just saving to a .mat file would be far better.
  2 件のコメント
Mr. Unkown
Mr. Unkown 2015 年 4 月 16 日
Thanks Adam, Yes I need to use it outside the Matlab. Can please expand your solution? what do you mean by doc fprintf?
Adam
Adam 2015 年 4 月 16 日
doc fprintf
just takes you to the help page for the function you need to use. There are examples there, but for your own struct you have to work out how to format the text file yourself.

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


Rob Campbell
Rob Campbell 2020 年 4 月 14 日
編集済み: Rob Campbell 2020 年 4 月 14 日
This might do what you want: https://github.com/ewiger/yamlmatlab
It handles nested structures too. For example, if I define a structure like this:
settings.main.medFiltRawImage = 5;
settings.main.doTiledMerge=true;
settings.main.tiledMergeThresh=1.05;
settings.main.secondExpansion=false;
settings.main.defaultThreshSD=7;
settings.mainBin.removeNoise = true;
settings.mainBin.medFiltBW = 5;
settings.mainBin.primaryShape = 'disk';
settings.mainBin.primaryFiltSize = 50;
settings.mainBin.expansionShape = 'square';
settings.mainBin.doExpansion = true;
settings.mainBin.expansionSize = 600;
settings.mainGetBB.minSizeInSqMicrons = 15000;
settings.mergeO.mergeThresh=1.3;
settings.stackStr.rescaleTo=50;
settings.autoThresh.skipMergeNROIThresh=10;
settings.autoThresh.doBinaryExpansion=false;
Then it will be saved to disk with yaml.WriteYaml whereupon it will look like:
main: {medFiltRawImage: 5.0, doTiledMerge: true, tiledMergeThresh: 1.05, secondExpansion: false,
defaultThreshSD: 7.0}
mainBin: {removeNoise: true, medFiltBW: 5.0, primaryShape: disk, primaryFiltSize: 50.0,
expansionShape: square, doExpansion: true, expansionSize: 600.0}
mainGetBB: {minSizeInSqMicrons: 15000.0}
mergeO: {mergeThresh: 1.3}
stackStr: {rescaleTo: 50.0}
autoThresh: {skipMergeNROIThresh: 10.0, doBinaryExpansion: false}

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by