xlsread error
조회 수: 22 (최근 30일)
이전 댓글 표시
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
답변 (8개)
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
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
2012년 4월 12일
댓글 수: 2
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.
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
댓글 수: 0
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
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
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
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
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
2017년 7월 18일
See https://support.microsoft.com/en-us/help/3001579/an-error-occurred-when-sending-commands-to-the-program-error-in-excel . In particular, this can be caused by having some of the Excel Add-in.
댓글 수: 0
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.
댓글 수: 0
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.
: )
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!