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);
For example, if the template code in your MATLAB script is:
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:
>>
>>
>>
>> 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:
Best Answer