%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % FileName:UAVState_Data_Exporter % Author: 森郷 % Created on: 2023年12月4日 % % Description: % このスクリプトは、UAVState構造体から時系列データを抽出し、 % CSVファイルに出力します。ヘッダーには各フィールド名が使用され、 % 行方向に時間毎の各データを記録しています。 % % Copyright (C) 2023 morisato % All rights reserved. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ヘッダーを抽出 headerFields = fieldnames(UAVState); headers = {'time'}; for i = 1:length(headerFields) subStructFields = fieldnames(UAVState.(headerFields{i})); for j = 1:length(subStructFields) headers{end+1} = [headerFields{i} '_' subStructFields{j}]; end end % ファイル名を定義 currentDateTime = datestr(now, 'yyyymmddHHMM'); fileName = ['UAVStateData_', currentDateTime, '.csv']; % ヘッダーをCSVファイルに書き込む fid = fopen(fileName, 'w'); fprintf(fid, '%s', strjoin(headers, ',')); fclose(fid); % データを格納する行列を初期化 row_num = length(UAVState.(headerFields{end}).(subStructFields{1}).Time); col_num = length(headers); dataMatrix = zeros(row_num, col_num); % 時間データを行列に格納 dataMatrix(:,1) = UAVState.(headerFields{end}).(subStructFields{1}).Time; % 各フィールドデータを行列に格納 dataIndex = 2; for i = 1:length(headerFields) subStruct = UAVState.(headerFields{i}); subStructFields = fieldnames(subStruct); for j = 1:length(subStructFields) dataMatrix(:, dataIndex) = subStruct.(subStructFields{j}).Data; dataIndex = dataIndex + 1; end end % データをCSVファイルに書き込む writematrix(dataMatrix, fileName, 'WriteMode', 'append'); % End