function newm( filename , displayFlag )
%NEWM - create new m-file(s) with headers, or add headers to existing
%m-file(s)
% This function will create a new m-file with the headers already there,
% automatically filling out the current date, Matlab version, file name (if
% specified), and Author (in Windows, pulls the current Windows user). If
% given no input, a new untitled document will appear with the header
% filled in. If given a filename that doesn't exist, the file is created.
% If given a filename that already exists, user is prompted to "Add
% Headers", "Add Headers to All", or "Skip File". Input filename(s) can be
% char or cell string.
%
% The headerString variable can be modified to reflect your own company or
% preferences.
%
% Syntax: newm('filename',displayFlag)
% Inputs:
% filename - Name of file(s) to create, or add headers to. Can be a char
% or a cell array of chars.
%
% displayFlag - Optional flag to suppress displaying the file(s). Would
% be most likely used to add headers to files in batch process.
%
% Outputs:
% none
%
% Examples:
% NewM
% NewM('NewFilename')
% NewM('NewFilename',false)
% NewM('NewFilename',true)
% NewM('ExistingFilename')
% NewM('ExistingFilename',false)
% NewM('ExistingFilename',true)
% NewM({'fname1','fname2','fname3'})
% NewM({'fname1','fname2','fname3'},false)
% NewM({'fname1','fname2','fname3'},true)
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also:
% Author: Mike Koelemay
% Work address:
% Email address: michael.koelemay@impact-tek.com
% Website: http://www.impact-tek.com
% 2007/10/01; Last revision: 2008/11/03
% Created with Matlab version: 7.5.0.342 (R2007b)
%
% The header format used in this file is from the "M-File Header Template"
% posted by Denis Gilbert on Matlab Central on May 12, 2004.
% Link: http://www.mathworks.com/matlabcentral/fileexchange/4908
% Link valid as of 11/3/2008.
%
%% Initialization
if nargin < 1
isNoInput = true;
filename = [cd '\untitled.m'];
else
isNoInput = false;
end
if nargin < 2 || isempty( displayFlag )
displayFlag = true;
end
if ~iscellstr( filename )
filename = {filename};
end
addHeadersToAll = false;
for fileLoop = 1:length( filename )
[p f] = fileparts(filename{fileLoop});
if isempty(p)
filename{fileLoop} = [cd '\' filename{fileLoop}];
end
if ~strcmp( filename{fileLoop}(end-1:end) , '.m' )
filename{fileLoop} = [filename{fileLoop} '.m'];
end
buttonOutput = '';
if ~addHeadersToAll && exist( filename{fileLoop} , 'file' )
buttonOutput = questdlg( [f ...
' already exists; what would you like to do?'],...
'WARNING - FILE ALREADY EXISTS!','Add Headers',...
'Add Headers to All','Skip File','Skip File');
end
addHeaders = false;
if addHeadersToAll
addHeaders = true;
else
switch buttonOutput
case 'Skip File'
continue
case 'Add Headers'
addHeaders = true;
case 'Add Headers to All'
addHeaders = true;
addHeadersToAll = true;
end
end
%% HEADER
headerString = {...
'function [output1] = <FILENAME>(input1)';
'%<FILENAMECAPS> - One line description of what the function or script performs (H1 line)';
'%Optional file header info (to give more details about the function than in the H1 line)';
'%';
'% Syntax: [output1] = <FILENAME>(input1)';
'% Inputs:'
'% input1 - Description'
'%'
'% Outputs:'
'% output1 - Description'
'%'
'% Example: '
'% Line 1 of example'
'%'
'% Other m-files required: none'
'% Subfunctions: none'
'% MAT-files required: none'
'%'
'% See also: OTHER_FUNCTION_NAME1'
''
'% Author: <AUTHOR>'
'% Work address:'
'% Email address: '
'% Website: http://www.'
'% <DATE>; Last revision: <DATE>'
'% Created with Matlab version: <VERSION>'
'%%'};
%% GET AUTHOR NAME, DATE, AND MATLAB VERSION
[junk author] = dos('echo %USERNAME%');
author = upper( author(1:end-1));
date = datestr(now,26);
ver = version;
%% FILL IN THE INFO
headerString = strrep( headerString , '<DATE>' , date );
headerString = strrep( headerString , '<FILENAME>' , f );
headerString = strrep( headerString , '<FILENAMECAPS>' , upper(f) );
headerString = strrep( headerString , '<AUTHOR>' , author );
headerString = strrep( headerString , '<VERSION>' , ver );
%% WRITE FILE and BRING UP IN EDITOR
if isNoInput
com.mathworks.mlservices.MLEditorServices.newDocument(...
sprintf( '%s\n', headerString{:} ));
else
if addHeaders
% make backup file in case something goes wrong
copyfile( filename{fileLoop} , [filename{fileLoop} '_backup'] );
end
if addHeaders
fid = fopen( filename{fileLoop} , 'rt' );
a = fscanf(fid, '%c' );
fclose( fid );
end
fid = fopen( filename{fileLoop} ,'wt' );
fprintf( fid, '%s\n' , headerString{:} );
if addHeaders
fprintf( fid , '%s' , a );
end
fclose( fid );
if addHeaders
% if all goes well, delete the backup file
delete( [filename{fileLoop} '_backup'] );
end
if displayFlag
edit( filename{fileLoop} );
end
end
end % fileLoop