Generate custom "New Script/Function" template
15 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2017-8-25
编辑: MathWorks Support Team
2019-12-19
Is there a way to modify the default script when creating a new one from the "new" pulldown (generate script/function template)?
采纳的回答
MathWorks Support Team
2019-12-19
编辑:MathWorks Support Team
2019-12-19
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:
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!