Saving struct to an Excel file
7 次查看(过去 30 天)
显示 更早的评论
I have several sets of MAT files that have a structure that I want to save to Excel (each in their own sheet) for some analysis. I tried writetable(struct2table(statistics), 'example.xls','sheet',1) but the first three fields cause index errors, which I figured would happen. I'm not too familiar with working with structures so I could use some help.
Below is the structure that I want to save.
statistics =
struct with fields:
revision: 'Rev 2789'
ymin: 1
ymax: 2501
peakToPeak: [48×1 double]
peakToPeakdB: [48×1 double]
variance: [48×1 double]
variancedB: [48×1 double]
peakFrequencyMHz: [48×1 double]
centerFrequency6dB: [48×1 double]
centerFrequency3dB: [48×1 double]
bandWidth6dB: [48×1 double]
bandWidth3dB: [48×1 double]
pulseLength10dB: [48×1 double]
pulseLength18dB: [48×1 double]
RMS: [48×1 double]
2 个评论
dpb
2018-9-18
What analysis do you think you can do more effectively in Excel than in Matlab? How about avoiding the problem by using ML instead and not have two separate tools for one job?
采纳的回答
Fangjun Jiang
2018-9-18
编辑:Fangjun Jiang
2018-9-18
You can take a look at the example in the document. Use rmfield() to remove the first three fields. Since all the other fields are 48x1 double, you can export all the data into one sheet, instead of multiple sheets.
doc struct2table
更多回答(1 个)
dpb
2018-9-18
编辑:dpb
2018-9-18
s.rev='Rev 1';
s.ymin=1;
s.pkpk=rand(3,1);
s.pkpkDB=rand(3,1);
>> struct2table(s,'AsArray',1)
ans =
1×4 table
rev ymin pkpk pkpkDB
_______ ____ ____________ ____________
'Rev 1' 1 [3×1 double] [3×1 double]
>>
writetable however, will make each individual element in each array a variable in the output file which will be extremely unwieldy.
>> writetable(ans)
>> type ans.txt
rev,ymin,pkpk_1,pkpk_2,pkpk_3,pkpkDB_1,pkpkDB_2,pkpkDB_3
Rev 1,1,0.93268309594783,0.334217197485708,0.934040921888512,0.28744527318977,0.492382824642905,0.468898718730782
>>
If you're adamant about doing this, probably easiest is either the rmfield option and handle the non-array fields separately or convert to cell array and then write the resulting array and non-array data--
>> c=struct2cell(s); % convert to cell array
>> c=c(3:end).'; % keep only the arrays as row array, not column
>> m=cell2mat(cc) % convert to array
ans =
0.9327 0.2874
0.3342 0.4924
0.9340 0.4689
>>
That array can be written w/ xlswrite. If you create header variable line and space for the non-array data, then with some effort can make a useful spreadsheet, but I'm still wondering "why? all the trouble?".
Just convert to a table and use all the power of Matlab.
In fact, why not recast the whole thing to use a table instead from the git-go?
2 个评论
dpb
2018-9-18
OK, I've "been there, done that!" in places where "rules are rules" whether they make sense or not.
Just figured I'd ask...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!