经常写matlab文件时,有时需要自己文件包含一些格式化信息,比如加上时间、自己的姓名、邮箱等信息,使得文件看起来更正规、更舒服。
下面贴出之前收藏的从网上找的 格式化matlab新建文件 的脚本。
包含两类文件,新建普通.m
文件和新建函数文件。
只要把下面的m
文件放到matlab能找到的系统路径里。
nm dummy
,就会自动生成一个dummy.m
文件,且里面有格式化的自定义的时间、邮箱等信息。
新建普通脚本文件
nm.m
function nm(mname)
% NM is the abbreviation of 'new M-File'
% NM create a MATLAB function with entered filename
%
% NM creates a M-File having the entered filename and a specific
% structure which helps for creating the main function structure. It
% would be opened and the starting line for writting will be highlighted
% (Only available in R14 or higher). The actual working MATLAB Version
% will be also captured. If user forgot to enter code and execute the
% function, he will get a reminder to enter code in the function.
%
% to create M-Files and Funcitons in a quick Way.
%
% Example :
% ---------
% >> NM dummy
%
% %%%%%%%%%% BEGINN CODE %%%%%%%%%%
% % DUMMY ...
% %
% % ...
%
% %% AUTHOR : xxx
% %% $DATE : 16-Oct-2014 13:29:27 $
% %% $Revision : 1.00 $
% %% DEVELOPED : 8.1.0.604 (R2013a)
% %% FILENAME : dummy.m
%
% % Write here...
%
% %% End_of_File
% % Created with NM.m by xxx
% % Contact...: xxxxx@163.com
% % ===== EOF ====== [dummy.m] ======
% %%%%%%%%%% CODE - END %%%%%%%%%%
%
if nargin == 0, help(mfilename); return; end
if nargin > 1, error(' MSG: Only one Parameter accepted!'); end
ex = exist(mname,'file'); % does M-Function already exist ? Loop statement
while ex == 2 % rechecking existence
msg = sprintf(['Sorry, but M-File -< %s.m >- does already exist!\n', ...
'Do you wish to Overwrite it ?'], mname);
% Action Question: Text, Title, Buttons and last one is the Default
action = questdlg(msg, ' Overwrite M-File?', 'Yes', 'No','No');
if strcmp(action,'Yes') == 1
ex = 0; % go out of While Loop, set breaking loop statement
else
% Dialog for new M-File name
mname = char(inputdlg('Enter new M-File Name ... ', 'NEWM - New Name'));
if isempty(mname) == 1 % {} = Cancel Button => "1"
disp(' MSG: User decided to Cancel !')
return
else
ex = exist(mname,'file'); % does new M-File exist ?
end
end
end
CreationMsg = CreateM(mname); % Call of Sub-Function
disp([' MSG: <' mname '.m> ' CreationMsg])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% CREATEM %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s = CreateM(name)
% Sub-Function will write the M-File, open it and mark the starting write
% line
ext = '.m'; % Default extension for a M-File !!
filename = [name ext];
fid = fopen(filename,'w');
str_tmp1 = upper(name);
h1 = ['% ', str_tmp1, ' ...']; % HELP-Line's will be preset
h2 = '% ';
h3 = '% ...';
fprintf(fid,'%s \n', h1); % First 3 Lines will be write in file
fprintf(fid,'%s \n', h2); % "
fprintf(fid,'%s \n\n', h3); % "
% Writer settings will be consructed ...
author = '%% AUTHOR : xxx';
dt = datestr(now); date = ['%% $DATE : ', dt, ' $'];
rev = '%% $Revision : 1.00 $';
devel = ['%% DEVELOPED : ',version];
filenamesaved = filename; fns = ['%% FILENAME : ', filenamesaved];
% Line 5-10 will be write in File ...
fprintf(fid,'%s \n', author);
fprintf(fid,'%s \n', date);
fprintf(fid,'%s \n', rev);
fprintf(fid,'%s \n', devel);
fprintf(fid,'%s \n\n', fns);
% Reminder that user must enter code in created File / Function
lst = '% Write here...';
fprintf(fid,'%s\n', lst);
% Before last line, from where functionality does come
originl0 = '%% End_of_File ';
originl1 = '% Created with NM.m by xxx ';
originl2 = '% Contact...: xxxxx@163.com ';
fprintf(fid,'\n\n\n%s \n%s \n', originl0,originl1);
fprintf(fid,'%s \n', originl2);
% Last Line in the M-File
end_of_file = ['% ===== EOF ====== [', filenamesaved, '] ====== '];
fprintf(fid,'%s \n', end_of_file);
% Close the written File
st = fclose(fid);
if st == 0 % "0" for successful
% Open the written File in the MATLAB Editor/Debugger
v = version;
if v(1) == '8' % R14 Version
opentoline(filename, 12); % Open File and highlight the start Line
else
% ... for another versions of MATLAB
edit(filename);
end
s = 'successfully done !!';
else
s = ' ERROR: Problems encounter while closing File!';
end
%% End_of_File
% Created with NFCN.m by xxx
% Contact...: xxxxx@163.com
% ===== EOF ====== [nm.m] ======
网友评论