ワークスペースにある​構造体配列をcsvフ​ァイルで保存する方法

217 views (last 30 days)
海成 本多
海成 本多 on 27 Aug 2021
Edited: John Doe on 4 Sep 2022
現在、以下の構造体配列がワークスペースにある状況で、これを新規のcsvファイルで保存したいのですがやり方がわかりません。
structPAcc1=16752*1 struct

Accepted Answer

Toru Ikegami
Toru Ikegami on 27 Aug 2021
こんにちは,
構造体配列を CSV に保存したいとのことですが,「構造体」のまま保存することは出来ないので,テーブル型に変換してから保存する事になるかと思います.前提として,構造体配列の要素の各フィールドのデータ型は等しいとします(x(1).a とx(2).a のデータ型が同じと言うことです.)
関数 struct2tablewritetable を使います.
構造体配列を準備します.
S.a = 1;
S.b = 2;
S(2) = S;
S(2).a = 3;
S(2).b = 4;
disp(S);
1×2 struct array with fields: a b
最初に struct2table で構造体配列をテーブルに変換します.構造体配列のフィールド名を列名とするテーブルが生成されます.
T = struct2table(S)
T = 2×2 table
a b _ _ 1 2 3 4
生成されたテーブルを関数 writetable を使ってファイルに書き出します.ファイル名の拡張子を .csv にすると,カンマ区切りのテキストファイルとして保存されます.
writetable(T, "fileName.csv");
  1 Comment
海成 本多
海成 本多 on 28 Aug 2021
解決しました。ご協力ありがとうございました。

Sign in to comment.

More Answers (1)

John  Doe
John Doe on 4 Sep 2022
Edited: John Doe on 4 Sep 2022
Food = 1
Restautant = 2
1 + 2 = Eating places

Categories

Find more on データ型の変換 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!