using actxserver
15 次查看(过去 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
0 个评论
回答(2 个)
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 个评论
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
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.
0 个评论
另请参阅
类别
在 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!