xlsread error

75 次查看(过去 30 天)
Sarah
Sarah 2012-4-12
Hey guys,
I can't seem to figure out why I get this error while trying to run some code. It is related to the xlsread function that I am using. Here is the code that I have:
[T5.Sig, T5.TStr] = xlsread('chambertest5.xlsx',1,'C5:K608469');
Here is the error that I get:
Source: Microsoft Office Excel
Description: Open method of Workbooks class failed
Help File: C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
Help Context ID: 0
Error in ==> xlsread at 250
ExcelWorkbook = Excel.workbooks.Open(file,0,true);
As you can see, I am trying to read a lot of data. Do you think that is the problem? Thanks for your time.
  2 个评论
Image Processing
Image Processing 2016-9-27
What is the size of your file ?
Dries
Dries 2017-7-18
217 kb

请先登录,再进行评论。

回答(8 个)

Greydon Gilmore
Greydon Gilmore 2017-8-22
Hi All,
Sorry for reviving an old post but I had this same issue.
I resolved it by running task manager and ending all excel running processes. I viewed the excel file I was attempting to open with Matlab just before running xlsread. Ending all processes through task manager seemed to solve the problem.
Cheers, Greydon
  3 个评论
dinesh sah
dinesh sah 2020-9-4
work for me too...
Image Analyst
Image Analyst 2020-9-6
Greydon, I think this should have been a comment to me, rather than a "new" answer", since I already mentioned this in my answer: "are you sure the file is not already open in Excel when you try to open it via your MATLAB code? Type ctrl-shift-Esc to check for any hidden Excel processes that may be running but not visible to you." This nice shortcut key sequence works in Windows only.

请先登录,再进行评论。


Sarah
Sarah 2012-4-12
anyone?
  2 个评论
Walter Roberson
Walter Roberson 2012-4-12
To determine whether the problem is in asking for too much data, temporarily change it to ask for only a little data.
I'm relatively sure you will find that the error stays even if you only ask for a little data.
Sarah
Sarah 2012-4-12
The error does stay even if I ask for little data. and I closed the excel file to see if that was causing a problem, and it still does.
any suggestions?

请先登录,再进行评论。


Eric
Eric 2012-4-12
I would try using the Excel COM interface directly to see where Excel is struggling. It's interesting that it seems to be failing when simply opening the worksheet. The following might let you test that.
First, close Excel. Make sure Excel is not running in the background by looking for any Excel.exe processes in Task Manager. Then...
XLS_COM = actxserver('Excel.Application');
XLS_COM.Visible = 1;%Make Excel Visible
WB_COM = XLS_COM.Workbooks.Open(filename);%Open the specified workbook - provide the full path
SheetOBJ = WB_COM.Sheets.Item(1);%Using the first worksheet
RangeOBJ = SheetOBJ.Range('C5:K608469');
data = RangeOBJ.Value;%Creates a cell array
You could also try reading data without using Workbooks.Open(). In that case, close Excel and make sure no instances of Excel are running. Then open Excel and then open your file. Make sure your workbook is the only one open. Then...
XLS_COM = actxGetRunningServer('Excel.Application');
WB_COM = XLS_COM.Workbooks.Item(1);
SheetOBJ = WB_COM.Sheets.Item(1);%Using the first worksheet
RangeOBJ = SheetOBJ.Range('C5:K608469');
data = RangeOBJ.Value;%Creates a cell array
If it really is Workbooks.Open() that's causing the problem, this second approach avoids the necessity of calling that method. You might want to test these out with smaller ranges as well.
Good luck,
Eric

Image Analyst
Image Analyst 2012-4-12
First of all, are you sure your worksheet actually has 608 thousand rows in it? If it does, then are you sure the file is not already open in Excel when you try to open it via your MATLAB code? Type ctrl-shift-Esc to check for any hidden Excel processes that may be running but not visible to you. If there is no instance of Excel running, then it could be a memory issue and you may have to read it in smaller chunks, or else try to delete() any temporary large arrays that are taking up memory in your function or any calling parent functions to try to free up memory.
Try this, where I check that the file exists and try to read in just a small part of the worksheet:
folder = pwd;
baseFileName = 'chambertest5.xlsx';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
else
message = sprintf('Success!\nWorkbook %s was found!', fullFileName);
uiwait(msgbox(message));
end
% If we get here, the file was found.
% Read a small chunk to test it.
% If it works, try reading the full worksheet.
[num, txt, raw] = xlsread(fullFileName ,1,'C5:K60');
  2 个评论
Sarah
Sarah 2012-4-12
I tried everything you said, but i still have no success!
btw, how am i supposed to know if the file was found or not? my output is:
basefilename = chambertest5.xlsx
fullfilename = chambertest5.xlsx
Image Analyst
Image Analyst 2012-4-12
I edited it so that it will now tell you if it found the workbook. Of course you could have also stepped through it with the debugger. But your workbook probably does exist. I just wanted to make sure it does exist in case it was not in your current folder and was somewhere else, in which case robust code like this will alert you to the fact rather than give some error that you don't understand or think is due to another reason.
So, were you able to read in a smaller chunk of the workbook?

请先登录,再进行评论。


Edward Umpfenbach
Edward Umpfenbach 2012-4-12
I had a different error with .xlsx read. I ended up saving my file as a .csv and reading it into matlab function csvread.
Doesn't really hit on the root of your issue but it might get you around it.
  3 个评论
Edward Umpfenbach
Edward Umpfenbach 2012-4-12
Agreed, but it was far easier than trying to debug the error. Saving as a csv cost me about 5 seconds. Here is the thread I was mentioning, if you are curious:
http://www.mathworks.com/matlabcentral/answers/12705-xlsread-only-works-once-per-matlab-session
Image Analyst
Image Analyst 2012-4-12
Well if you're just using it as a way to store data and won't ever really deal with it in Excel, then a CSV file will be much, much faster anyway, since you don't have to launch Excel and shutdown Excel every time you want to write out some data.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-7-18

Varun Bansal
Varun Bansal 2019-3-5
I solved this issue by doing --
Make a Desktop folder in C:\Windows\SysWOW64\config\systemprofile and C:\Windows\System32\config\systemprofile
This will only work, if you are using a SYSTEM account to access excel, In my case, I am doing this excel automation on a build server.
Thanks MS Windows, generating bugs and breads for developers.

Scott Hazell
Scott Hazell 2020-3-13
编辑:Scott Hazell 2020-3-13
I had this same error and did the old IT crowd turn it off and on again (closed matlab and re open) and it worked.
Hope this helps.
: )

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by