How can i create a standard matlab template for new programs

50 ビュー (過去 30 日間)
DoVile Last Name:
DoVile Last Name: 2012 年 12 月 15 日
編集済み: Sayyed Ahmad 2018 年 6 月 18 日
I like to have the same layout of my code every time i create a new program, at the moment i just copy and paste an .m file. But is there any way it start with the following comments and code each time i make a new file matlab file ?
%
% Description:
%
% Author: Thor P. Nielsen
%
% Date: XX-XX-20XX
%
% Comment:
%
% Tests run:
clear; close; clc
ps. if you have any suggestions for more "stuff" i could/should include in every programme please let me know :)
  1 件のコメント
Sayyed Ahmad
Sayyed Ahmad 2018 年 6 月 18 日
編集済み: Sayyed Ahmad 2018 年 6 月 18 日
I creat some function which is called: CLASS_Template.m and used some KEY_CLASSNAME as a key in this template.
{
%codes of CLASS_Template:
classdef test
properties (SetAccess='public', GetAccess='public')
prop;
end
properties (SetAccess='private', GetAccess='public')
end
properties(SetAccess='private', GetAccess='private')
end
methods
function o=test(varargin)
switch nargin
case 1
o.prop = varargin{1};
otherwise
error('the number of input proprties doesn''t macht.')
end
end
function display(o)
disp('<a href="matlab: helpwin test">test properties:</a>')
disp(o.prop);
end
function disp(o)
display(o)
end
function help(o)
helpwin test
end
end
end
}
in the second file called CreateClass would the Key replaced with the className.
{
function CreateClass(varargin)
classname = varargin{1};
dirname = ['@' classname];
str_date=datestr(date,'dd.mmm.yyyy');
[status,message,messageid]=mkdir(dirname);
pause(1);
fid = fopen('/Path_to_Template/CLASS_Template.m');
F = fread(fid, '*char')';
fclose(fid);
F=strrep(F, 'KEY_CLASSNAME', classname);
F=strrep(F, 'KEY_DATE', str_date);
cd([pwd '\' dirname]);
fid = fopen([classname '.m'], 'w');
fwrite(fid, F);
fclose(fid);
open([classname '.m'])
end
}
Now you can run the following command
{
>>CreateClass test
}
Depending on your needs you could have more than one key.
I hope you have your answer.
cheers! Ahmad

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 12 月 15 日
編集済み: Matt Fig 2012 年 12 月 15 日
You could put that code in an m-file then use COPYFILE to copy it to a new m-file, including the intended name.
Say your above template is saved as func_template.m and you want to make a new function named myfunc.m. Save this:
function [] = make_fun(V)
copyfile('func_template.m',V)
edit(V)
Then from the command line:
>> make_fun('myfunc.m')
  4 件のコメント
DoVile Last Name:
DoVile Last Name: 2012 年 12 月 15 日
Thank you Matt, works perfectly!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2012 年 12 月 15 日
Here are some lines of code that you might consider putting at the top of your test scripts. Pick and choose which you want:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
  1 件のコメント
Richard Crozier
Richard Crozier 2018 年 4 月 23 日
編集済み: Richard Crozier 2018 年 4 月 23 日
Don't ever put clear or clc at the top of your test scripts. You'll regret it some day (or someone you work with will regret it). If you need a clean workspace, put the test in a function.

サインインしてコメントする。

カテゴリ

Find more on Search Path 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!

Translated by