MATLAB Answers

Read or write in the .xlsx files

262 views (last 30 days)
Itachi
Itachi on 19 Aug 2012
Commented: Image Analyst on 1 May 2015
Hi guys, I've written a code with alot of loops and in each loop there the xlsread or xlswrite commands. I've installed Excel 2010 and I'm not using it when I'm running the code . These are the errors:
It may be locked by another process.
or
Error registering event(s), Advise failed
Error in registerevent>addevent (line 148)
list(m+1) = handle.listener(h, eventname, {@comeventcallback, eventhandler});
Error in registerevent (line 94)
addevent(h, event, eventhandler);
Error in xlsreadCOM (line 11)
Excel.registerevent({'WorkbookActivate', @WorkbookActivateHandler});
Error in xlsread (line 230)
[numericData, textData, rawData, customOutput] = xlsreadCOM(file, sheet, range,
Excel, customFun);
Please help me out. thanks in advance.

  4 Comments

Show 1 older comment
Itachi
Itachi on 19 Aug 2012
It's happening because I'm using too much xlsread or xlswrite functions in the code, But why is that?! Thanks
José-Luis
José-Luis on 19 Aug 2012
Every time you launch xlswrite/read, excel is launched, processes your commands and closes. That is heavy business. It is hard to guess what might cause your problem without knowing the structure of your code, what kind of data you have, etc... So for others to help, more information is needed. A recommendation on my part would be to look at memory comsumption when your process is running, if it spikes then maybe you have too many calls to that function, but that's just a wild guess.
Itachi
Itachi on 19 Aug 2012
Dear Guerrero, Your guess is correct. I use the xlsread/write to read/write vectors from/into the Excel file too many times. Is there no way to tell MATLAB to open the file and close it whenever the code is finished?

Sign in to comment.

Accepted Answer

José-Luis
José-Luis on 19 Aug 2012
I'll make it an answer.
I think you can, see the link:
Cheers!

  2 Comments

Itachi
Itachi on 19 Aug 2012
Thank you, that's exactly what I've been looking for.
Suraj Srivastava
Suraj Srivastava on 30 Apr 2015
Hi,
Colud you please tell me that, How can I use xlswrite1 in GUI. I have used it, but it gives error.
Any comment is appreciated.
Thanks, Suraj

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 19 Aug 2012
My guess is that you already have Excel open with that workbook. If something strange happened, like you used ActiveX and hid Excel but never closed it, then Excel may still be running even if you can't see it in the task bar. In that case, if you're using Windows, type control-shift-Esc to bring up the process list, and kill Excel.

  0 Comments

Sign in to comment.


Suraj Srivastava
Suraj Srivastava on 30 Apr 2015
Hi,
Colud you please tell me that, How can I use xlswrite1 in GUI. I have used it, but it gives error.
Any comment is appreciated.
Thanks, Suraj

  3 Comments

Suraj Srivastava
Suraj Srivastava on 1 May 2015
Hi, Thanks for your valuable reply. The thing is that I am using xlsread and xlswrite functions so many times in my GUI code. And I am using it in one of my optimization process. The code runs smoothly but taking very long time. Also if I give 3000 iterations to run then for all the 3000 simulations it runs well but at the end it gives error. But if run the same for 50 iterations it did'nt gives any error. I tried using xlswrite11 function, it runs for normal coding but in GUI it gives error. Kindly suggest me what I should to improve the speed of my code or to rectify the error or how to use xlswrite1 function(As it is not clear to me from ExcelDemo.m)
Kindly help me out.
Thanks, Suraj
Image Analyst
Image Analyst on 1 May 2015
Of course it takes a long time. You're launching Excel 3000 times and shutting down Excel 3000 times. It's going to take like a trillion billion years to do that - maybe longer! That's why I gave you an Active X solution that uses xlswrite1() that will do this in just a few seconds. I encourage you to learn it. It's not that hard. If a big dummy like me could figure it out, I'm sure a brilliant engineer like you can too. If you don't like my well commented tutorial, then you can check out the Mathworks ActiveX tutorials instead. xlswrite1() assumes you've used ActiveX to create an Excel object in advance - you can do this with just one line of code, though it would be better to make it more robust and use a few lines of code to handle situation like Excel is already running, it already has your workbook open, etc.

Sign in to comment.

Sign in to answer this question.


Translated by