How can i create a standard matlab template for new programs

29 Ansichten (letzte 30 Tage)
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 Kommentar
Sayyed Ahmad
Sayyed Ahmad am 18 Jun. 2018
Bearbeitet: Sayyed Ahmad am 18 Jun. 2018
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')
properties (SetAccess='private', GetAccess='public')
properties(SetAccess='private', GetAccess='private')
function o=test(varargin)
switch nargin
case 1
o.prop = varargin{1};
error('the number of input proprties doesn''t macht.')
function display(o)
disp('<a href="matlab: helpwin test">test properties:</a>')
function disp(o)
function help(o)
helpwin test
in the second file called CreateClass would the Key replaced with the className.
function CreateClass(varargin)
classname = varargin{1};
dirname = ['@' classname];
fid = fopen('/Path_to_Template/CLASS_Template.m');
F = fread(fid, '*char')';
F=strrep(F, 'KEY_CLASSNAME', classname);
F=strrep(F, 'KEY_DATE', str_date);
cd([pwd '\' dirname]);
fid = fopen([classname '.m'], 'w');
fwrite(fid, F);
open([classname '.m'])
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt Fig
Matt Fig am 15 Dez. 2012
Bearbeitet: Matt Fig am 15 Dez. 2012
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)
Then from the command line:
>> make_fun('myfunc.m')
  4 Kommentare
Matt Fig
Matt Fig am 15 Dez. 2012
Bearbeitet: Matt Fig am 15 Dez. 2012
In it's own function m-file. In other words, type:
then when the new window pops up, paste in only that code and save it. So basically you will have the template file and the above, seperate file that copies the template to a new file whenever you want.
DoVile Last Name:
DoVile Last Name: am 15 Dez. 2012
Thank you Matt, works perfectly!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 15 Dez. 2012
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.
% 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.
  1 Kommentar
Richard Crozier
Richard Crozier am 23 Apr. 2018
Bearbeitet: Richard Crozier am 23 Apr. 2018
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.

Melden Sie sich an, um zu kommentieren.


Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by