格式化matlab文件02_新建函数文件

经常写matlab文件时,有时需要自己文件包含一些格式化信息,比如加上时间、自己的姓名、邮箱等信息,使得文件看起来更正规、更舒服。

下面贴出之前收藏的从网上找的 格式化matlab新建文件 的脚本。

包含两类文件,新建普通.m文件和新建函数文件。

只要把下面的m文件放到matlab能找到的系统路径里。
nfcn dummy,就会自动生成一个dummy.m函数文件,且里面有格式化的自定义的时间、邮箱等信息。

新建函数文件

nfcn.m

function nfcn(fcnname)
%   NFCN is the abbreviation of 'new funtion File'
%   NFCN create a MATLAB function with entered filename
%
%   NFCN creates a Function-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.
%
%   NM, NFCN and FRN are a set of M-Functions which shold help the MATLAB User 
%   to create M-Files and Funcitons in a quick Way.
%
%   Example :
%   ---------
%   >> NFCN dummy
%
%   %%%%%%%%%% BEGINN CODE %%%%%%%%%%
%     function []=dummy()
%     % DUMMY ...
%     %
%     %   ...
% 
%     %% AUTHOR    : Ben
%     %% $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 NEWM.m by Ben  
%     % Contact...: xxxxx@163.com  
%     % ===== EOF ====== [dummy.m] ======     
%   %%%%%%%%%% CODE - END  %%%%%%%%%%
%
%   See also: NEWM FRN

%% AUTHOR    : Ben 
%% $DATE     : 16-Oct-2014 13:29:27 $
%% $Revision : 2.00 $
%               1.change name: from 'newfcn' to 'nfcn'
%% FILENAME  : NFCN.m 
%% Note      : This File is wrote by modifying 
%                Frank Gonzalez-Morphy's File(newfcn.m)
%% 

if nargin == 0, help(mfilename); return; end
if nargin > 1, error('  MSG: Only one Parameter accepted!'); end


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 ... ', 'NFCN - New Name'));
        if isempty(fcnname) == 1  % {} = Cancel Button => "1"
            disp('   MSG: User decided to Cancel !')
            return
        else
            ex = exist(fcnname,'file');  % does new functionname exist ?
        end
    end
end

CreationMsg = CreateFcn(fcnname);   % Call of Sub-Function
disp(['   MSG: <' fcnname '.m> ' CreationMsg])



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%   CREATEFCN   %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s = CreateFcn(name)
% Sub-Function will write the M-File, open it and mark the starting write
% line

ext = '.m';  % Default extension for a FUNCTION !!
filename = [name ext];

fid = fopen(filename,'w');

line_1 = ['function [] = ',name,'()'];    % Function Header

str_tmp1 = upper(name);
h1 = ['% ', str_tmp1, ' ...'];          % HELP-Line's will be preset
h2 = '% ';
h3 = '%   ...';

fprintf(fid,'%s\n', line_1);            % First 4 Lines will be write in file
fprintf(fid,'%s \n', h1);               %   "
fprintf(fid,'%s \n', h2);               %   "
fprintf(fid,'%s \n', h3);               %   "
fprintf(fid,'%s \n', h3);             %   "
fprintf(fid,'%s \n\n', h2);               %   "

% Writer settings will be consructed ...
author = '%% AUTHOR    : Ben';
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 NFCN.m by Ben ';
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 Fcn
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, 13);  % 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   
% Contact...: xxxxx@163.com  
% ===== EOF ====== [nfcn.m] ======  

    原文作者:book_02
    原文地址: https://www.jianshu.com/p/66b467fa7e01
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞