Saving struct to an Excel file

8 vues (au cours des 30 derniers jours)
Pete Marsh
Pete Marsh le 18 Sep 2018
Commenté : dpb le 18 Sep 2018
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 commentaires
dpb
dpb le 18 Sep 2018
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 le 18 Sep 2018
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.

Connectez-vous pour commenter.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 18 Sep 2018
Modifié(e) : Fangjun Jiang le 18 Sep 2018
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 commentaire
Pete Marsh
Pete Marsh le 18 Sep 2018
That will work. Thanks.

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 18 Sep 2018
Modifié(e) : dpb le 18 Sep 2018
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 commentaires
Pete Marsh
Pete Marsh le 18 Sep 2018
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 le 18 Sep 2018
OK, I've "been there, done that!" in places where "rules are rules" whether they make sense or not.
Just figured I'd ask...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by