MATLAB: Import a cell array in excel

actxserver

Each cell contain other elements:
and a single element can contain some value like a matrix or a date:
Now i want to put all this cell array (called EXCEL) in Excel…but it doesn't show me the data and the matrices…what I did:
% MATLAB Automation client example
% Open Excel, add workbook, change active worksheet,
% get/put array, save.
% First, open an Excel Server.
e = actxserver('Excel.Application');
% Insert a new workbook.
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
% Make the first sheet active.
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate;
% Put a MATLAB array into Excel.
for i=1:N
EXCEL{i}
eActivesheetRange = e.Activesheet.get('Range','A1:G7');
eActivesheetRange.Value =EXCEL{i};
end
% Get back a range.
% It will be a cell array, since the cell range
% can contain different types of data.
eRange = e.Activesheet.get('Range', 'A1:G7');
B = eRange.Value;
% Convert to a double matrix. The cell array must contain only
% scalars.
%B = reshape([B{:}], size(B));
% Now, save the workbook.
%eWorkbook.SaveAs('C:\Users\Folder\myfile.csv'); %or .xls
% Avoid saving the workbook and being prompted to do so
eWorkbook.Saved = 1;
eWorkbook.Close;
% Quit Excel and delete the server.
e.Quit;
e.delete;
It appears me:
even if matlab recognize that i passed [240×320] double and a Data but excel doesn't.
I would like obtain in excel like the first link in which i can click and see the sub element of each cell.
Regarding, Vincenzo

Best Answer

Like you are aware of, when you write the data to the spreadsheet, every cell in the spreadsheet must contain a scalar number (or a string). It can not be a cell array.
The problem with your data is that you have a cell array that has nested cell array. Even worse, the size of each cell array is not consistent. So you couldn't even use cell2mat() to convert your cell array to a big matrix and then write to the spreadsheet.
It is still possible to write the data to a spreadsheet, although I doubt that you can make it behave like the workspace editor you saw in MATLAB (your first link).
You'll need to go through a loop, check the class of each element of the cell array. If the element is another cell array, you'll need to go deep. If it's not a cell array, you can write it to the spreadsheet. You'll need to determine its size and the position of the spreadsheet to write in. The function iscell() is what you need.
Run celldisp(EXCEL) and also take a look at the provided source code celldisp.m. I think you can modify it to solve your problem.
In your current code, you are over-writing Range 'A1:G7' again and again. You need to vary the range string in the loop, such as RangeString=['A',num2str(i)].
I don't understand the code below "%Get back a range".