Import a cell array in excel
10 次查看(过去 30 天)
显示 更早的评论
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 [240x320] 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
1 个评论
Walter Roberson
2011-10-31
I do not recall ever hearing that Excel could support nested structures ? As far as I know, you can pass in a cell array but each element of the array has to be able to fit in to one Excel cell ?
采纳的回答
Fangjun Jiang
2011-10-31
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".
0 个评论
更多回答(3 个)
Vincenzo
2011-11-1
1 个评论
Fangjun Jiang
2011-11-1
Just write the first row,
eActivesheetRange = e.Activesheet.get('Range','A1:C1');
eActivesheetRange.Value={'Header1','Header2','Header3'};
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!