필터 지우기
필터 지우기

matlab structure to csv file

조회 수: 11 (최근 30일)
Sanjay Zamindar
Sanjay Zamindar 2022년 2월 16일
편집: Jan 2022년 2월 17일
I have a struct like below
header: {1x6 cell}
dates: 202201
data: [0.9779 0.2625 -0.0108 0.1753 -0.0049 -0.0172]
AssetID: 'PAR'
where
K>> coef.header
ans =
Columns 1 through 5
'mexico' 'france' 'berlin' 'italy' 'china'
Column 6
'srilanka'
I want to take output of this in .csv file like
Any help or guidance will be really appricated

채택된 답변

Jan
Jan 2022년 2월 16일
편집: Jan 2022년 2월 17일
% [EDITED] Adjusted for comma separators:
coef.header = {'mexico', 'france', 'berlin', 'italy', 'china', 'srilanka'};
coef.dates = 202201;
coef.data = [0.9779 0.2625 -0.0108 0.1753 -0.0049 -0.0172];
coef.AssetID = 'PAR';
[fid, msg] = fopen('C:\Folder\YourFile.csv', 'w');
assert(fid > 0, msg); % No file opening without checking the success!
fprintf(fid, '%s', 'dates');
fprintf(fid, ',%s', 'AssetID', coef.header{:});
fprintf(fid, '\n');
fprintf(fid, '%i,%s', coef.dates, coef.AssetID);
fprintf(fid, ',%g', coef.data);
fprintf(fid, '\n');
fclose(fid);
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 2월 17일
Each place Jan had a \t change that to a comma.
Jan
Jan 2022년 2월 17일
편집: Jan 2022년 2월 17일
@Sanjay Zamindar: I've modified the code. A problem with the commas is that they should not appear after the last element of each row.
With tabs as separators, a trailing tab is okay usually.
There are smarter methods to export an CSV file, e.g. writematrix and a lot of tools in the FileExchange. I wanted to suggest a simple "wooden" method, because it can be adjusted easily.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Risk Management Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by