Matlab - How to Delete Empty Sheets within Excel Workbook
06 Nov 2007 Quan Quach 14 comments 1,676 views
One annoying thing about using the xlswrite command in Matlab is that it automatically generates three sheets whether you want them or not. Even if you write the data onto a specifically named sheet, Matlab still creates the Excel file with those three extra sheets! The following function will allow you to automatically delete any sheets that are empty. Copy and paste the code below into your Matlab Editor and save it as an .m file. Next, change your current directory in Matlab to the location of the .m file. Now, call the function and insert the full path of the Excel file as the input argument, like this:
deleteEmptyExcelSheets('C:\Documents and Settings\matlab\myExcelFile.xls')
%created by: Quan Quach %date: 11/6/07 %this function erases any empty sheets in an excel document function deleteEmptyExcelSheets(fileName) %the input fileName is the entire path of the file %for example, fileName = 'C:\Documents and Settings\matlab\myExcelFile.xls' excelObj = actxserver('Excel.Application'); %opens up an excel object excelWorkbook = excelObj.workbooks.Open(fileName); worksheets = excelObj.sheets; %total number of sheets in workbook numSheets = worksheets.Count; count=1; for x=1:numSheets %stores the current number of sheets in the workbook %this number will change if sheets are deleted temp = worksheets.count; %if there's only one sheet left, we must leave it or else %there will be an error. if (temp == 1) break; end %this command will only delete the sheet if it is empty worksheets.Item(count).Delete; %if a sheet was not deleted, we move on to the next one %by incrementing the count variable if (temp == worksheets.count) count = count + 1; end end excelWorkbook.Save; excelWorkbook.Close(false); excelObj.Quit; delete(excelObj);
14 Responses to “Matlab - How to Delete Empty Sheets within Excel Workbook”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Very useful! Thanks for sharing
Hi
I would like to know simulation running on web using matlab. Do you have
any information on that?Thanks
Regards
I would like to know how I can modified the content of a xl file i.e. how i can delete any row and then save the file.
And also how to delete the reqired sheets from excel workbook?
Thanks
Hello,
i would like to ask about excel in matlab. how can i link the excel database in order to view the data on my matlab gui? i just need to display the excel data on my gui..
Thanks.
[...] Original post by adelia [...]
this is very useful
how can I paste 3 or more figure to one excel worksheet? I can paste one figure to a sheet but any more and I only can paste the last figure if I have more than one figure.
W
to zayhla:
for simulation running on web through MATLAB, you can refer to MATLAB Builder for Java or .NET. You can get the interactive graphics or simulation using the web browser.
hi,i am working excel files with matlab and i need help with a problem.
my excel file has a value in every sheet which i store them to a vector V ,also each sheet includes real times like(13:45:05 etc etc) with a time interval like TI=3 sec(basically is float number cause it depends from the number of sheets).Anyway i would like to average every value in my vector V but for 360sec.
For example if my duration is 3600sec=360*10 i would like to average every 360 sec the values of vector V until time elapsed.this code is only for the first 360sec any ideas how i could make it move further?
ty in advice
love this script…:)…many thanks for sharing….
I’m trying to delete the extra xml spread sheets but i’m getting the following error
??? Invoke Error, Dispatch Exception:
Source: Microsoft Office Excel
Description: ‘testPreProcess.xls’ could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.
Help File: C:\Program Files\Microsoft Office\OFFICE11\1033\xlmain11.chm
Help Context ID: 0
Error in ==> deleteEmptyExcelSheets at 10
excelWorkbook = excelObj.workbooks.Open(fileName);
Error in ==> preSort at 221
deleteEmptyExcelSheets(writeFileName)
any ideas how to get ride of the error
@ Anonymous
I’ve just recreated this issue myself, generating the same error message. I’ve corrected the issue by using the full file path name (i.e. C:\Documents and Settings\My Documents\Sub Directory\testPreProcess.xls)
Give that a shot!
Thank you for sharing this function
It is very useful
Thank you for this last hint. Very useful function!