Import a cell array in excel

10 visualizaciones (últimos 30 días)
Vincenzo
Vincenzo el 31 de Oct. de 2011
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 comentario
Walter Roberson
Walter Roberson el 31 de Oct. de 2011
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 ?

Iniciar sesión para comentar.

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 31 de Oct. de 2011
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".

Más respuestas (3)

Vincenzo
Vincenzo el 31 de Oct. de 2011
Oh no!! That's a bad new.
Ok consider just the scalar values...If I do the for loop before it shows me
but as you can see are just the last value repeated from A1:G7
Indeed it show me all the value but in a millisecond it overwrite the precedent (always from A1:G7) with the next one ,till the last.
Instead i want that i see all the values from A1:G1 a raw of a value, A2:G2 another row of values and so on...how can i do this in a for loop?

Vincenzo
Vincenzo el 1 de Nov. de 2011
Thank you Walter and Fangjun
Just another thing: how can I assign a name to the top of a column?
  1 comentario
Fangjun Jiang
Fangjun Jiang el 1 de Nov. de 2011
Just write the first row,
eActivesheetRange = e.Activesheet.get('Range','A1:C1');
eActivesheetRange.Value={'Header1','Header2','Header3'};

Iniciar sesión para comentar.


Vincenzo
Vincenzo el 1 de Nov. de 2011
It Works, it works!!! Thank you!!

Categorías

Más información sobre Use COM Objects in MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by