From: Thomas Britton on 6 Apr 2010 10:14 Yes - you can handle excel far quicker and more efficiently if you write the functions yourself (or adapt the xlsread and xlswrite functions to your needs). There is significant overhead if you are having to open the activex server each time you want to read something from a large data set. Try looking inside xlswrite and xlread for specific functions/handles. You can type: edit xlswrite and it will open the function up in the editor (make sure you save the file under another name before you begin your edit!) You'll need to find the bit that has: Excel = actxserver('Excel.Application'); At line 161 or thereabouts.
From: james bejon on 6 Apr 2010 10:18 Carl, I think the "help" you talk of is really just knowing how VB interacts with Excel. So, perhaps you should write the basic routine in a VBA module first (using the VB Help function) and then move it to an m-file later? Regarding having to quit instances all the time: the try-catch routine I mentioned earlier should do the job. Have a look at this thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/278382#732709
From: Kirill on 6 Apr 2010 10:38 On Apr 6, 9:43 am, "Carl " <carl.me...(a)scottish-southern.co.uk> wrote: > Thanks James, that is perfect. > > While I want the version of excel to be invisible in the final version making it visible is perfect for while I am developing it. > > On a related issue is there any help available to handling excel within Matlab. So far a I just used other people's code but I am sure I could get my code to run faster by adapting it. > > For example xlswrite1 is fine for writing lots of variables int oa single spreadsheet, but I am wirting to several. Currently I do this by invoking excel several times, It must be possble (and faster) after saving a file to close it and open another, instead of quitting excel and invoking it again. I just can't find the correct syntax. Here is a sample code how to write a matrix into Excel workbook. It either attaches to a running instance or opens a new one. If there is plethora of small matrices to write in opening a new Excel instance slows process of data transfer considerably. Excel.ActiveSheet.Range size should be consistent with matrix dimensions. I have somewhere a function to translate matrix coordinates to Excel range, will try to find it later. Kirill % writing matrix m (the largest one) to Excel workbook % excelactivex15.m tic % Open new instance of Excel or attach to the running one try Excel = actxGetRunningServer('Excel.Application'); flgRunning = 1; catch Excel = actxserver('Excel.Application'); flgRunning = 0; end % open workbook m = rand(65536, 256); Excel.Workbooks.Open('C:\matlab.xls') % must exists Excel.ActiveSheet.Cells.Clear; % clear entire sheet r = Excel.ActiveSheet.Range('A1:IV65536'); r.Value = m; r.release; Excel.ActiveWorkbook.Save; if flgRunning == 0 Excel.set('DisplayAlerts', 0); Excel.Quit; Excel.delete; end toc
From: Carl on 6 Apr 2010 11:47
Thanks this seems to make things clearer. The code I had seen before had statements such as: invoke(Excel.ActiveWorkbook,'Save'); which I was trying to convert to a close statement and failing. If I can just write: Excel.[VBAcode] that makes things a lot easier. By the way is there a significant benefit using an already running version of excel (other than reduced memory usage and the time to open the session once). Often while running a long matlab script I will go into excel and play with some spreadsheets, at such times I don't want Matlab to take over my session. |