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 CenterFile 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!

Translated by