Zanam
Zanam

Reputation: 4807

Printing from Matlab to Excel

I am creating a dummy example for my situation. Real problem is way more complicated.

Dummy Example:

I have a struct of size 2 with an entry called 'name' and an array of size 2x1.

So, the Struct called Store looks like:

Store(1).name = 'Apple';
Store(1).Data = [1; 2];

Store(2).name = 'Orange';
Store(2).Data = [24; 57];

I want to print this to excel such that it looks like the following:

Apple     1
Apple     2
Orange    24
Orange    57

I don't even know how to start with the above example. I have used xlswrite in the past but never for mixed type data.

Upvotes: 0

Views: 90

Answers (1)

sebastian
sebastian

Reputation: 9696

If all your Data fields are of size 2x1, you should be able to the following:

% prepare cell to hold your data
xlData = cell(2*numel(store), 2);
% fill column 1:
xlData(1:2:end-1,1) = {store.name};
xlData(2:2:end,1) = xlData(1:2:end-1);
% fill column 2:
xlData(:,2) = num2cell(vertcat(store.Data));
% write to excel:
xlswrite('yourExcelfile.xlsx', xlData)

Got no Matlab at hand to test, but it should help you get going.

Since xlswrite takes a cell-array, there's no issue with mixed datatypes.

Upvotes: 3

Related Questions