XlsWrite keeps overwriting my excel file!

2 views (last 30 days)
Ellis Berry
Ellis Berry on 29 Jun 2016
Edited: Sudhanshu Bhatt on 22 Jul 2016
Hi everybody,
My GUI is very nearly complete but I have one last slight issue. Everytime my image processing is complete, my GUI saves the results to an excel file using xlswrite. This works fine, but as I am error checking my code, the situation could arise where the user wants to run the code again to check if the results are right for example. If this happens, the previous excel file is overwritten with the new results. I want my code to ensure a new excel file is made and the old one is not overwritten, how do I do this? Many thanks for taking your time to answer. Here is the section of code in question:
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.outDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
theFiles=dir(filePattern);
caListBoxItems = cell(length(theFiles), 1);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
thisString = fprintf(1, 'Now reading %s', fullFileName);
fprintf('%s\n', thisString);
caListBoxItems{k} = thisString;
OutputFileNames{k} = theFiles(k).name;
set(handles.listbox2, 'String', OutputFileNames); %listbox2 will display file names of processed images to listbox2.
drawnow; % Force immediate screen repaint.
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
time=((k-1)*Interval);
if black>(numel(image)*0.0193); %if black pixels is more than 1.93% of pixels in image, experiment complete. Value can be altered.
disp('The experiment is complete at this stage!')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
disp('Time of Picture (Secs): ');
disp((k-1)*Interval); %Here, "Interval" is a variable chosen by user (15 secs, 30 secs etc)
Status = ('Complete');
Date = sprintf(datestr(now));
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
Status = ('Incomplete');
Date = sprintf(datestr(now));
end
PhotoDetails={fullFileName, black, time, Status, Date}; %Date variable here will save the exact time and date the images were processed in answer to line 228 and 234.
Matrix(k,:)=PhotoDetails; %Matrix of three variables produced.
guidata(hObject,handles);
end
end
Header={'Image', 'Number of Black Pixels', 'Time (Seconds)', 'Status', 'Date'}; %Headers that will appear in adjacent cells on excel spread.
xlswrite('PhotoResults', Header, 'Results');
xlRange='A2';
xlswrite('PhotoResults', Matrix, 'Results', xlRange);
  1 Comment
José-Luis
José-Luis on 29 Jun 2016
  1. Check if the file already exists: exist(). If it does, create a new one.
  2. Or save what you need in another sheet within the existing file: please read the documentation to understand how to do that.

Sign in to comment.

Answers (1)

Sudhanshu Bhatt
Sudhanshu Bhatt on 22 Jul 2016
Edited: Sudhanshu Bhatt on 22 Jul 2016
Hi Ellis,
As suggested above you can use the sample code snippet below to save in a new Excel file everytime:
for counter=1:5
excelFileName = ['myFileName' num2str(counter) '.xlsx']
xlswrite(excelFileName, 1:10);
end
Also, you can create a new sheet everytime you run the program.See the sample code below on how to do this:
for counter=1:5
xlswrite('myExcelFile.xlsx', 1:10, ['Sheet' num2str(counter)]);
end
More information on xlswrite can be found int he documentation page below:
Hope this help !!
Thanks
Sudhanshu Bhatt

Community Treasure Hunt

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

Start Hunting!