How to get xlswrite to create a file that uses the next available filename?
1 view (last 30 days)
Show older comments
How can I get xlswrite to create a file that does not overwrite the current file? I have it set to save the filename with the date incorporated. I want to add a number on the end so the file does not get overwritten. I want the number to increase over the day (set a variable that stays that variable for the day) then I want the number to reset if it is a different day.
what I have so far:
global dataFolder;
global name;
global trial;
global xlsfile;
try
trial
catch
trial = 1;
end
if isempty(trial)
trial = 1;
end
file = strcat(dataFolder, name, '_', datestr(date,'dd-mm-yyyy'), '_CRM',num2str(trial),'.xls');
if exist(file)==2
trial = trial +1;
file=strcat(dataFolder, name, '_', datestr(date,'dd-mm-yyyy'), '_CRM',num2str(trial),'.xls');
end
xlsfile = file;
0 Comments
Accepted Answer
Jan
on 25 Sep 2013
Global variables are a frequent source of errors, which are hard to debug. Providing the data as input and output arguments of a function is smarter.
fullfile is smarter for the creation of file names than strcat, because it cares for the correct file separators.
exist(file) is prone to unexpected errors and slow, because it searches in the complete Matlab path for files and folders, names of variables and Java classes. Better use exist(file, 'file').
function File = GetUniqueFile(Folder, File)
Today = datestr(now, 'dd-mm-yyyy');
[dummy, Name, Ext] = fileparts(File);
Existing = dir(fullfile(Folder, [Name, '_', Today, '_CRM*', Ext]));
nExisting = length(Existing);
File = fullfile(Folder, [Name, '_', Today, sprintf('_CRM%.03d', nExisting), Ext]);
3 Comments
More Answers (0)
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!