using actxserver

18 ビュー (過去 30 日間)
ricco
ricco 2011 年 11 月 30 日
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

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2011 年 11 月 30 日
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 件のコメント
ricco
ricco 2011 年 11 月 30 日
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 2011 年 11 月 30 日
You'll probably need to use:
MyRange=Sheets.range('A1:C1') or
MyRange=Sheets.UsedRange;
Then:
Data=MyRange.Value;

サインインしてコメントする。


Image Analyst
Image Analyst 2011 年 11 月 30 日
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.

カテゴリ

Help Center および File ExchangeUse COM Objects in MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by