How to delete data in excel file?

5 views (last 30 days)
How to delete data in excel file from Guide? Thanks!

Accepted Answer

Image Analyst
Image Analyst on 5 Jun 2012
Use ActiveX programming. Several examples of using ActiveX to control Excel are in this Answers forum and in the Mathworks help or web site. I haven't cleared cells exactly yet. But I've cleared comments from cells and the code looks like this:
%---------------------------------------------------------------------------------------------------------------------
% Add comments to cells on sheet.
% Sometimes this throws exception #0x800A03EC on the second and subsequent images. It looks like this:
% "Error: Object returned error code: 0x800A03EC"
% It is because of trying to insert a comment for a worksheet cell when a comment already exists for that worksheet cell.
% So in that case, rather than deleting the comment and then inserting it, I'll just let it throw the exception
% but I won't pop up any warning message for the user.
function InsertComments(Excel, caComments, sheetNumber, startingRow, startingColumn)
try
worksheets = Excel.sheets;
% thisSheet = get(worksheets, 'Item', sheetNumber);
thisSheet = Excel.ActiveSheet;
thisSheetsName = Excel.ActiveSheet.Name; % For info only.
numberOfComments = size(caComments, 1); % # rows
for columnNumber = 1 : numberOfComments
columnLetterCode = cell2mat(ExcelCol(startingColumn + columnNumber - 1));
% Get the comment for this row.
myComment = sprintf('%s', caComments{columnNumber});
% Get a reference to the cell at this row in column A.
cellReference = sprintf('%s%d', columnLetterCode, startingRow);
theCell = thisSheet.Range(cellReference);
% You need to clear any existing comment or else the AddComment method will throw an exception.
theCell.ClearComments();
% Add the comment to the cell.
theCell.AddComment(myComment);
end
catch ME
errorMessage = sprintf('Error in function InsertComments.\n\nError Message:\n%s', ME.message);
fprintf(errorMessage);
WarnUser(errorMessage);
end
return; % from InsertComments
The above is a nice little function if you have a list of comments that you want to add to the cells. They will popup when you hover the cursor over them. To clear a cell, my guess is that you'd probably do something like
theCell.Clear();
but you might have to check the API reference or play around with it a little bit.
  3 Comments
Image Analyst
Image Analyst on 5 Jun 2012
if exist(fullFileName, 'file')
delete(fullFileName);
end
dinh tai
dinh tai on 5 Jun 2012
yes. Спасибо ! :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!