필터 지우기
필터 지우기

Saving struct to an Excel file

조회 수: 2 (최근 30일)
Pete Marsh
Pete Marsh 2018년 9월 18일
댓글: dpb 2018년 9월 18일
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
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?
Pete Marsh
Pete Marsh 2018년 9월 18일
This is acoustic test data and most of our tools for the analysis is in Excel. Since some of the others that will have to do some processing on it do not have Matlab.

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

채택된 답변

Fangjun Jiang
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
Pete Marsh
Pete Marsh 2018년 9월 18일
That will work. Thanks.

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

추가 답변 (1개)

dpb
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
Pete Marsh
Pete Marsh 2018년 9월 18일
Unfortunately, I have been given the code that creates the MAT files that I have to use (not by choice), so I have very little latitude with what I can do. And since it must be shared with several others, I have been tasked to put them into Excel.
Thanks for the input.
dpb
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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by