Saving a structure to Excel

93 views (last 30 days)
I have a simple (albeit large) structure that follows the pattern Year.Variable.Month.TimeSlot
There are 8 TimeSlot values, and when opened each one displays a 16x1 array. I'm looking for something that will enable me to save all 8 of these values into excel (i.e. saving a single month of data). If there was a way to save each month onto the same Excel file, this would be great, however I don't mind a little manual cutting and pasting to put all the months together on a single sheet.
Any help would be massively appreciated.

Accepted Answer

David Sanchez
David Sanchez on 13 Oct 2014
Try this out (adapt it to your need)
a.b.c.d = rand(16,1);
a.b.c.e = rand(16,1);
my_last_field = fieldnames(a.b.c);
% write the last field values in a single matrix
L = numel(my_last_field);
my_data = zeros(16,L);
for k=1:L
my_data(:,k) = a.b.c.(my_last_field{k});
end
% write the matrix to excell sheet
xlswrite('text.xls',my_data)
  1 Comment
Gareth Pritchard
Gareth Pritchard on 13 Oct 2014
This was a great help, thanks!

Sign in to comment.

More Answers (2)

David Sanchez
David Sanchez on 13 Oct 2014
take a look at this linK:
Where is clearly stated:
You may need to use struct2cell() to convert your structure to cell array and then use xlswrite(). Please give an example of your structure. There might be special things to deal with.
  1 Comment
Gareth Pritchard
Gareth Pritchard on 13 Oct 2014
Thanks, but I've tried using that and it's not much help. Basically I'm looking to save the last field in Excel for the entire structure

Sign in to comment.


Anmol Pardeshi
Anmol Pardeshi on 9 Feb 2018
If struct to cell and then conversion is a problem, you can individually collect the fields and their values in a matrix (table) and then export it to excel. I have a struct with 8 field and the last field has a string matrix of n-by-2 dimension. the way I solved this problem was - a. start a new matrix (table) b. collect the 1st, 2nd, 3rd field in cols 1/2/3 rectvly., of this new table. c. in the 4th & 5th col, I copied the n-by-2 (or essentially the 2 cols of last field in struct corresponded to the last 2 cols of the new table)
the new table is ought to increase in length because it becomes something like a table that expands in the last col i.e. something like the diagram of the FFT butterfly.
I can share the code if anyone needs it. :)
  1 Comment
raghvendra deshpande
raghvendra deshpande on 6 Mar 2018
Yes .. Please share the code

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!