Prev: Use filename
Next: video compression using matlab code
From: james bejon on 3 Apr 2010 06:24 Dear All, It says here (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/brd05nl.html#f134682) that Matlab can connect to an existing Excel workbook via 1. excelapp = actxserver('Excel.Application'); 2. wkbk = excelapp.Workbooks; 3. wdata = wkbk.Open('d:\weatherlog\weekly_log.xls'); But I can't get this to work. Moreover, I can't even see how it COULD work, since Line 1 creates a new INSTANCE of Excel (as can be verified by making excelapp visible--via excelapp.visible = 1), meaning Line 3 opens a new (read-only) copy of 'weekly_log.xls' rather than providing an interface with an existing workbook. Have I misread something somewhere? P.S. The reason I want to do this is so I can write data to an already-open workbook.
From: james bejon on 3 Apr 2010 08:49 In case anyone else finds this useful, I've come up with a solution. It's not very pretty, but I think it works. % ------------------------------------ % WRITE TO XLS % ------------------------------------ try % If there's an EXISTING INSTANCE of EXCEL, then use it xlAPP = actxGetRunningServer('Excel.Application'); bXLNewInst = 0; catch % Otherwise, create a NEW INSTANCE xlAPP = actxserver('Excel.Application'); bXLNewInst = 1; end % SET-UP the App (Make it VISIBLE and NON-PROMPTING) xlAPP.DisplayAlerts = 0; if ~xlAPP.Visible xlAPP.Visible = 1; end try % If the FILE's ALREADY OPEN, then reference it xlFILE = xlAPP.get('Workbooks', sFILE_NAME); bXLNewFile = 0; catch % OTHERWISE, OPEN the file xlFILE = xlAPP.Workbooks.Open([sROOT_PATH, sFILE_NAME]); bXLNewFile = 1; end % DO ALL YOUR PROCESSING AND WRITING AND STUFF HERE % ... % ... % ------------ % TIDY UP % ------------ % If a NEW FILE was OPENED, then SAVE & CLOSE it if bXLNewFile xlFILE.SaveAs([sROOT_PATH, sFILE_NAME]); xlFILE.Close; end % If a NEW INSTANCE of Excel was created, then QUIT it if bXLNewInst xlAPP.Quit; xlAPP.delete; end
From: james bejon on 3 Apr 2010 08:53 P.S. Should have mentioned: the sFILE_NAME is JUST the name, i.e. it doesn't have an attaching PATH.
|
Pages: 1 Prev: Use filename Next: video compression using matlab code |