Generate custom "New Script/Function" template

15 views (last 30 days)
Is there a way to modify the default script when creating a new one from the "new" pulldown (generate script/function template)?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 Dec 2019
Edited: MathWorks Support Team on 19 Dec 2019
1) The preferred method is the following:
You can use the API listed below to create a new document with pre-populated text:
>> matlab.desktop.editor.newDocument(text); % `text` is the character array to pre-populate upon opening up the editor
For example, if the template code in your MATLAB script is:
% New Script
clc
clear
Then the corresponding command using the given API would be:
>> matlab.desktop.editor.newDocument(['% New Script' newline 'clc' newline 'clear']);
To integrate this command in your workflow, you could create a Favorite Command (via Favorites \ New Favorite) and use it as a button on the Quick Access Toolbar. That way you could simply click the button to create a new script with the desired template code.
2) Otherwise you can use the method listed below:
You can accomplish this workflow using the following steps:
1. Create a script “my_template.m” that has the layout of your code
For example:
>> % Description:
>> % Author: Foo
>> % Comment:
>> close all; clear; clc
2. Create a function "make_fun.m" that utilizes "copyfile" function to copy the template to a new m-file
>> function [] = make_fun(V)
>> copyfile('my_template.m',V)
>> edit(V)
3. From the command line, you can call "make_fun" function to create a new function/script with the name of your choice
>> make_fun('newScript.m');
This solution was originally provided in the following link:

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!