当想这些已经新建的文件进行重命名时,可以使用下面的函数frn.m
来保持格式,只是修改其中的名字信息。
重命名已有文件
function frn(oldname,newname)
% FRN is the abbreviation of 'function rename'
% FRN is a function which help to rename completely a function
%
% FRN is a helper function which helps you not only to rename
% the filename, also the different name labels inside the function.
% In specialy these file works for created Functions with NFCN.m
% You must provide first the Filename which should be renamed and as
% second parameter the new File- and Functionname.
% NOTE: To be able to use these Function to rename a Function, it must
% have at least (see also Example below):
% * Function Name Header
%
% NFCN, NMand FRN are a set of M-Functions which shold help
% the MATLAB user to create Functions in a quick way.
%
% Syntax: >> frn('OldFcnName','NEWFcnName')
% >> frn OldFcnName NEWFcnName
%
% See also: NM NFCN
%
% Example:
% =========== >> nfcn dummy ===============================
% function dummy()
% % DUMMY ...
%
% %% FILENAME : dummy.m
%
% % [snip]
% % ===== EOF ====== [dummy.m] ======
% ===========================================================
%
% =========== >> frn dummy new_fcn_name ===========
% function new_fcn_name()
% % NEW_FCN_NAME ...
%
% %% FILENAME : new_fcn_name.m
%
% % [snip]
% % ===== EOF ====== [new_fcn_name.m] ======
% ===========================================================
%
%% AUTHOR : Ben
%% $DATE : 02-Nov-2014 15:27:26 $
%% $Revision : 1.00 $
%% DEVELOPED : 8.1.0.604 (R2013a)
%% Note : This File is wrote by modifying
% Frank Gonzalez-Morphy's File(newfcn_rename.m)
%% FILENAME : frn.m
if nargin == 0, help(mfilename); return, end
if nargin == 1
error(' MSG: You must enter at least the New Function Name!')
end
%check if the file of oldname exists
try
oldname = CheckIfFileNoExists(oldname);
catch
error(' MSG: FRN: CheckIfFileNoExists - After rechecking run again!')
end
%check if the oldname is empty
if isempty(oldname) == 1
disp(' MSG: User decided to Cancel !')
return;
end
%check if the file of newname exists
try
newname = CheckIfFileExists(newname);
catch
error(' MSG: FRN:CheckIfFileExists - After rechecking run again!')
end
%check if the newname is empty
if isempty(newname) == 1
disp(' MSG: User decided to Cancel !')
return;
end
ext_mfile = '.m';
oldname_ext = [oldname ext_mfile];
newname_ext = [newname ext_mfile];
% Open File for Read content
fid = fopen(oldname_ext, 'r');
% Scaned_File = fread(fid, '*char')
Scaned_File = fscanf(fid, '%c', inf);
fclose(fid);
% Modified_File = Scaned_File;
% Different Sizes of the Names
% ... OLD
sz_old = size(oldname,2);
sz_old_ext = size(oldname_ext,2);
% ... NEW
% sz_new = size(newname,2);
% sz_new_ext = size(newname_ext,2);
% Upper Filenames
oldname_U = upper(oldname);
newname_U = upper(newname);
try
% Position of function Header
% Start_Pos_fH = findstr([oldname '('], Scaned_File);
Start_Pos_fH = strfind(Scaned_File, [oldname '(']);
% Start_Pos_fH = Start_Pos_fH(1); % (1) because we have 3 OldNames in File
% Renaming function Header
if isempty(Start_Pos_fH) ~= 1
Start_Pos_fH = Start_Pos_fH(1);
Modified_File = [Scaned_File(1,1:Start_Pos_fH-1) newname, ...
Scaned_File(1,Start_Pos_fH+sz_old:end)];
else
error(' MSG: Sorry, we can only convert function File, check your file !');
end
catch
% Position of function Header, there is a blank character between...
% oldname and (
% Start_Pos_fH = findstr([oldname ' ('], Scaned_File);
Start_Pos_fH = strfind(Scaned_File, [oldname ' (']);
% Start_Pos_fH = Start_Pos_fH(1); % (1) because we have 3 OldNames in File
% Renaming function Header
if isempty(Start_Pos_fH) ~= 1
Start_Pos_fH = Start_Pos_fH(1);
Modified_File = [Scaned_File(1,1:Start_Pos_fH-1) newname, ...
Scaned_File(1,Start_Pos_fH+sz_old:end)];
else
error(' MSG: Sorry, we can only convert function File, check your file !');
end
end
% Position of upper Help Header
% Start_Pos_uHH = findstr(oldname_U, Modified_File);
Start_Pos_uHH = strfind(Modified_File, oldname_U);
% Renaming upper Help Header
if isempty(Start_Pos_uHH) ~= 1
Modified_File = [Modified_File(1,1:Start_Pos_uHH-1) newname_U, ...
Modified_File(1,Start_Pos_uHH+sz_old:end)];
end
% Position of Filename Line
% Start_Pos_ext = findstr(oldname_ext, Modified_File);
Start_Pos_ext = strfind(Modified_File, oldname_ext);
if isempty(Start_Pos_ext) ~= 1
Start_Pos_FN = Start_Pos_ext(1); % (1) because we have 2 *.m Name labels
% Renaming Filename Line
Modified_File = [Modified_File(1,1:Start_Pos_FN-1) newname_ext, ...
Modified_File(1,Start_Pos_FN+sz_old_ext:end)];
end
% Position of EOF Line
% Start_Pos_ext = findstr(oldname_ext, Modified_File);
Start_Pos_ext = strfind(Modified_File, oldname_ext);
if isempty(Start_Pos_ext) ~= 1
Start_Pos_EOF = Start_Pos_ext(end); % if there are more name in between
% Renaming EOF Line
Modified_File = [Modified_File(1,1:Start_Pos_EOF-1) newname_ext, ...
Modified_File(1,Start_Pos_EOF+sz_old_ext:end)];
end
% Delete the old Function File
delete(oldname_ext);
% Open File for Writing
fid = fopen(newname_ext, 'w+');
% Create and Write content into the File
fprintf(fid, '%s', Modified_File);
% fprintf(fid, '%c', Modified_File);
st = fclose(fid);
if st == 0
disp([' MSG: <' oldname '.m> renamed in :'])
disp([' MSG: <' newname '.m> successfuly renamed!'])
edit(newname_ext); % Edit the new Function on the Editor
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% CheckIfFileExists %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function notexisting = CheckIfFileExists(fcnname)
% Sub-Function does check if File exist already, if so ask for overriding
% it or maybe save it under another name.
ex = exist(fcnname,'file'); % does M-Function already exist ? Loop statement
while ex == 2 % rechecking existence
msg = sprintf(['Sorry, but Function -< %s.m >- does already exist!\n', ...
'Do you wish to Overwrite it ?'], fcnname);
% Action Question: Text, Title, Buttons and last one is the Default
action = questdlg(msg, ' Overwrite Function?', 'Yes', 'No','No');
if strcmp(action,'Yes') == 1
ex = 0; % go out of While Loop, set breaking loop statement
else
% Dialog for new Functionname
fcnname = char(inputdlg('Enter new Function Name ... ', 'NEWFCN - New Name'));
if isempty(fcnname) == 1 % {} = Cancel Button => "1"
% error(' MSG: User decided to Cancel !')
break;
else
ex = exist(fcnname,'file'); % does new functionname exist ?
end
end
end
notexisting = fcnname;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% CheckIfFileNoExists %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function notexisting = CheckIfFileNoExists(fcnname)
% Sub-Function does check if File exist already, if so ask for overriding
% it or maybe save it under another name.
ex = exist(fcnname,'file'); % does M-Function already exist ? Loop statement
while ex ~= 2 % rechecking existence
msg = sprintf(['Sorry, but Function -< %s.m >- does not exist!\n', ...
'Do you wish to Choose another one ?'], fcnname);
% Action Question: Text, Title, Buttons and first one is the Default
action = questdlg(msg, ' Choose Another?', 'Yes', 'No','Yes');
if strcmp(action,'Yes') == 1
% Dialog for new Functionname
fcnname = char(inputdlg('Enter new Function Name ... ', 'NEWFCN - New Name'));
if isempty(fcnname) == 1 % {} = Cancel Button => "1"
% error(' MSG: User decided to Cancel !')
break;
else
ex = exist(fcnname,'file'); % does new functionname exist ?
end
else
fcnname = '';
break; % go out of While Loop, set breaking loop statement
end
end
notexisting = fcnname;
%% End_of_File
% Created with NFCN.m by Ben
% Contact...:
% ===== EOF ====== [frn.m] ======