構造体のデータとフィールド名を Excel ファイルにエクスポートすることはできますか?
9 views (last 30 days)
Show older comments
MATLAB 上で構造体データとして格納しているデータがあります。
この変数を Excelのスプレッドシート上にエクスポートしたいのですが、例えば、構造体のフィールド名を 1列に、その隣の列には、対応する数値データ、さらにこれらの 2 列に対応するヘッダ行を追加する方法を教えてください。
Accepted Answer
MathWorks Support Team
on 6 Mar 2017
通常、XLSWRITE 関数を使って Excel ファイルにデータをエクスポートします。
構造体に定義された(数値)データと、フィールド名、ヘッダ行などの文字列を1つの変数としてまとめるために、出力したいデータを「セル配列」として定義します。
以下に、構造体のデータとフィールド名を Excel ファイルに書き出す方法を示します。
例1: データを結合して 1 つのシートに出力する例(.XLS に保存)
% サンプルデータ
x = struct('a', 1, 'b', 2, 'c', 3, 'd', 4, 'e', 5 );
y = struct('a', 10, 'b', 20, 'c', 30, 'd', 40, 'e', 50 );
% 出力データをセル配列として定義
c = [ {'フィールド名'} {'XValues'} {'Yvalues'} ;...
fieldnames(x) struct2cell(x) struct2cell(y)];
s = xlswrite('tempdata.xls', c) % Excelへの書き出し
例2: 各フィールドのデータを別のシート名で出力する例(.XLSX に保存)
data.a = rand(1,1e2);
data.b = rand(1,1e2);
Fname = fieldnames(data);
for n = 1:length(Fname)
xlswrite('data.xlsx', data.(Fname{n})(:),Fname{n})
end
なお、MATLAB Central には、構造体を CSV ファイルに出力するプログラムが公開されています。
・MATLAB Central: struct2csv
<https://jp.mathworks.com/matlabcentral/fileexchange/34889-struct2csv https://jp.mathworks.com/matlabcentral/fileexchange/34889-struct2csv
>ただし、MATLAB Centralにおいてフリーで公開されているファイルの内容に関しましては、直接プログラム作成者の方にお問い合わせください。
0 Comments
More Answers (0)
See Also
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!