how do I clear excel content of an old file before saving the news results to it?

43 views (last 30 days)
I am new to Matlab and just wrote small script to calculate the cooling cycle for my device. I would like to know how can I clear contents of the excel file i created before posting new results to it when its over written.
For ex, when I run my code for 1500 temp degree, I get two columns from A1:B50
now when I run again with temp of 1200, I get the results in the same excel but overwritten with range A1:B40... however, i will still see B40:B50 from the old results which is very confusing.
so I would like to know of a away that before i save the results, clear all the contents i had previously in the created excel so I can fill it with my new results.
here is my code
Code:
clear all; close all; clc;
% this program calculates the cooling cycle temps for
prompt = 'What''s your temperature? ';
x = input(prompt);
sheetnumber = 'Enter sheet number where results will be stored.. ';
sheetnum = input(sheetnumber);
filename = 'temp_increaments.xlsx';
y = x/30; % this is to calculate the stepsize based on 30 mins interval
z = round(y); % rounding the answer to cloeset integer so i can ..
% preallocate bcoz preallocation needs integers.
difference= zeros(1 ,z); % preallocated to save memory
for i = 2:z % *in this sense, 50 steps means every almost 35 seconds..
% .., i decrease another 5% (cooling increament).
x(i) = x(i-1)*0.95; % here is my equation, i use...
% the output of last first calc as the input of second cal
% and so on for 30 steps.
difference(i) = x(i-1) - x(i);
end
format longG
disp(x)
y = x.'; % to save results as column
change= sort(difference, 'descend'); % sorted the results from high temp..
% to low temp so i can see how much i need to change every cooling cycle
disp(change)
z= change.'; % same concept as saving y
%------------------------------------------------------------------------
sheet = sheetnum;
xlRange = 'A1';
xlswrite(filename,[y, z], sheet,xlRange)

Accepted Answer

Image Analyst
Image Analyst on 17 Dec 2018
What I do is to simply delete the file and then write the new one:
recycle on % Send to recycle bin instead of permanently deleting.
delete(filename); % Delete (send to recycle bin).
% Now write new file with none of the old/existing stuff in there.
xlswrite(filename, ...............
  3 Comments
Nuclear researcher
Nuclear researcher on 18 Dec 2018
I liked the idead of deleting the file and writing a new one. this is good for some uese; however, if for example, as shown in my code where i can save the results in different sheets, i want to keep sheet1 and write my new results in sheet 2. in this case if i use your code, then i will lose the info in sheet one.
I would be most grateful if you can chip in more ideas.
Image Analyst
Image Analyst on 18 Dec 2018
How comfortable are you with ActiveX programming? I'm attaching a demo. If you're using Windows, you can have ActiveX do basically absolutely anything that you'd be able to do withing Excel. You can drive Excel from MATLAB to do things like clear cells, format cells, delete worksheets, add worksheets, etc. I attach a demo.
Here is a method of a class I wrote, Excel_utils, to delete a worksheet:
%---------------------------------------------------------------------------------------------------------------------
% DeleteExcelSheets: deletes sheets in the active workbook that have their name specified.
% This function loops through all sheets and deletes those sheets whose name is in the caSheetNames cell array list.
% Excel_utils.DeleteExcelSheets(Excel, {'Results1', 'Results2'});
function DeleteExcelSheets(excelObject, caSheetNames)
try
% excelObject = actxserver('Excel.Application');
% excelWorkbook = excelObject.workbooks.Open(fileName);
% Run Yair's program http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object
% to see what methods and properties the Excel object has.
% uiinspect(excelObject);
worksheets = excelObject.sheets;
initialNumberOfSheets = worksheets.Count;
% Prevent beeps from sounding if we try to delete a non-empty worksheet.
excelObject.EnableSound = false;
% Tell it to not ask you for confirmation to delete the sheet(s).
excelObject.DisplayAlerts = false;
% Loop over all the names
for k = 1 : length(caSheetNames)
% Get the current number of sheets in the workbook.
preDeleteSheetCount = worksheets.count;
% There must always be at least one worksheet in an xls-file, so the last sheet must not be deleted.
if preDeleteSheetCount <= 1
break;
end
% Loop over all the currently existing sheets, looking for this name.
for sheetIndex = 1 : preDeleteSheetCount
% Activate the worksheet. (Perhaps unnecessary.)
% worksheets.Item(sheetIndex).Activate;
% Get the name of the worksheet with this sheet index.
thisName = worksheets.Item(sheetIndex).Name;
% See if this name is in the caSheetNames list.
itsInTheList = ismember(thisName, caSheetNames);
% If it's in the list, delete it.
if itsInTheList
worksheets.Item(sheetIndex).Delete;
% postDeleteSheetCount = worksheets.count;
% fprintf('%d sheets left in workbook\n', postDeleteSheetCount);
break;
end
end
end
% End up with the first sheet activated.
worksheets.Item(1).Activate;
excelObject.EnableSound = true;
catch ME
errorMessage = sprintf('Error in function DeleteExcelSheets.\n\nError Message:\n%s', ME.message);
fprintf('%s\n', errorMessage);
% WarnUser(errorMessage);
end
return; % from DeleteExcelSheets
end % of DeleteExcelSheets

Sign in to comment.

More Answers (1)

Bob Thompson
Bob Thompson on 18 Dec 2018
One option for doing this is to specify your maximum range all the time, rather than adjusting to a specific range. Anything beyond what is actually data will be printed as N/A. If that doesn't work for you then you could write a condition to print the rest of the cells as blank, either by creating a cell array in matlab, or by more individually printing the results.
xlswrite(filename,[y,z],sheet,xlRange);
if xlRange < maxRange
xlswrite(filename,[],sheet,[:,xlRange+1:maxRange]); % This range definition is for ideas only. It will not work as a command
xlswrite(filename,[],sheet,[xlRange+1:maxRange,1:xlRange]);
end

Community Treasure Hunt

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

Start Hunting!