xlsread error

50 views (last 30 days)
Sarah
Sarah on 12 Apr 2012
Commented: Image Analyst on 6 Sep 2020
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 Comments
Dries
Dries on 18 Jul 2017
217 kb

Sign in to comment.

Answers (8)

Greydon Gilmore
Greydon Gilmore on 22 Aug 2017
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 Comments
Image Analyst
Image Analyst on 6 Sep 2020
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.

Sign in to comment.


Sarah
Sarah on 12 Apr 2012
anyone?
  2 Comments
Sarah
Sarah on 12 Apr 2012
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?

Sign in to comment.


Eric
Eric on 12 Apr 2012
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 on 12 Apr 2012
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 Comments
Image Analyst
Image Analyst on 12 Apr 2012
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?

Sign in to comment.


Edward Umpfenbach
Edward Umpfenbach on 12 Apr 2012
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 Comments
Image Analyst
Image Analyst on 12 Apr 2012
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.

Sign in to comment.


Walter Roberson
Walter Roberson on 18 Jul 2017

Varun Bansal
Varun Bansal on 5 Mar 2019
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 on 13 Mar 2020
Edited: Scott Hazell on 13 Mar 2020
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.
: )

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by