using actxserver

18 visualizaciones (últimos 30 días)
ricco
ricco el 30 de Nov. de 2011
I have imported data from excel into matlab where I have several spreadsheets. I have used:
files = dir('*.xls');
%read data from excel into matlab
for i=1:length(files);
File_Name{i}=files(i,1).name;%Removes the file names from 'files'
[num{i},txt{i},raw{i}] = xlsread(File_Name{i},'Ble min');
end
So, each cell refers to each spreadsheet. Is there a quicker way of doing this? I attempted using actxserver, but unfortunately I dont know enough matlab to be able to do it. I tried
filenames = dir('*.xls');
%read data from excel into matlab
hExcel = actxserver('Excel.Application');
hExcel.visible = 1; % If you want Excel visible.
hExcel.DisplayAlerts = false; % Avoid excel warning popups
for i=1:length(filenames);
Wkbk = hExcel.Workbooks.Open(fullfile(pathTofiel,...
filenames{i})); % Opens Excel file
Sheets = Wkbk.Sheets('Bass min'); % Get the Sheets
Wkbk.Save; Save xls file
Wkbk.SaveAs('out.csv', 6);
Wkbk.Close;
end
hExcel.Quit;
hExcel.delete;
An error appears 'Cell contents reference from a non-cell array object'. Can anyone point me in the right direction?
thanks

Respuestas (2)

Fangjun Jiang
Fangjun Jiang el 30 de Nov. de 2011
filenames returned by dir() is a structure array, not a cell array. Try it alone to understand how filenames look like. To get the file name, you will need to use filenames(i).name
Excel COM server can be much faster than xlsread() when you have multiple files to open/process. See this post for some leads.
  2 comentarios
ricco
ricco el 30 de Nov. de 2011
many thanks. Will the following line return all of the data in the sheet 'Bass min' or will I need to alter this.
Sheets = Wkbk.Sheets('Bass min'); %
I see many examples of coding for this type of problem where the user has to specify the range from the excel worksheet!
Fangjun Jiang
Fangjun Jiang el 30 de Nov. de 2011
You'll probably need to use:
MyRange=Sheets.range('A1:C1') or
MyRange=Sheets.UsedRange;
Then:
Data=MyRange.Value;

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 30 de Nov. de 2011
This is not valid syntax:
Wkbk.Save; Save xls file
You'd need a % after the semicolon.
I'd recommend that you use csvwrite() if you want to save a csv file. It should be pretty fast, probably even faster than having Excel do it. But you need to get your data into an array. Your bottom chunk of code doesn't do anything - it merely opens a file and then saves it immediately, and then tries to save who-know-what (I guess the sheet called "Bass min"?) into a csv file, which of course won't work the way you have it because you're not saving any csv data - you're trying to save a complicated workbook as a simple text file which it doesn't know how to convert.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by