How to create a new m file automatically with filled in commands every time?

66 次查看(过去 30 天)
I wanted to have the ability of creating m files with some standard lines filled in like clear all; close all; datestr(now) every time I create a new file so that I do not have to enter them manually.
So I created a function mcreate(x) where x is the new m file I want to create. I have written the function like this.
function mcreate(x)
edit x
fid = fopen([x '.m'],'w');
fprintf(fid,'clear all;\nclose all;\n clc;\ndatestr(now);\n');fclose(fid);
a=[x '.m']
type a
delete x.m
Suppose now I invoke mcreate(ssrr)at the command prompt, it is creating a file called ssrr.m with the required lines. But it also is opening an editor window called x.m - which is unwanted. Also by type a wanted it to type ssrr.m but it is trying to type a that does not exist so it is giving an error to that effect.
I wanted to close the editor with x.m also too. how to close that window? Is there an opposite of 'open' command?
Thanks in advance.
Seetha Rama Raju Sanapala

采纳的回答

Guillaume
Guillaume 2014-12-5
Use function syntax instead of command syntax:
function mcreate(scriptname) %and use variable names that have meaning
scriptname = sprintf('%s.m', scriptname);
fid = fopen(scriptname, 'wt'); %and use 't' with text files so eol are properly translated
fprintf(fid, 'clear all;\nclose all;\nclc;\ndatestr(now);\n');
fclose(fid);
edit(scriptname);
%don't understand the purpose of these two lines:
type(scriptname);
delete(scriptname);
end
  6 个评论
Seetha Rama Raju Sanapala
No. I am a beginner. And it took me some time to understand what you mean by short cuts. I could check your code. And it is working very well. Thank you so much for educating me. I am happy with the answer given previously by Guillaume too which was already accepted as the answer. Functionally both are OK. Your method has the advantage that we do not have to remember the function.
Image Analyst
Image Analyst 2014-12-7
Go to the Shortcuts tab/ribbon. There is a little button there for "New shortcut". Paste in your/my code into the edit box. Check the checkbox at the bottom that says "Add to quick access toolbar". Then you will have that function available all the time with just a single click on the toolbar button. Try it and let me know how it worked.

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2014-12-5
I would just take the following code and paste it into a "shortcut" button on your tool ribbon so that it's always handy:
% Code to create a new file with default lines of code pre-entered into it.
% Get the name of the file that the user wants to save.
startingFolder = userpath
defaultFileName = fullfile(startingFolder, '*.m');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% Open a new file.
fileID = fopen(fullFileName, 'wt');
% Load it up with the lines of code you want in it.
fprintf(fileID, 'clc;\n');
fprintf(fileID, 'close all;\n');
fprintf(fileID, 'clear;\n');
fprintf(fileID, 'workspace;\n');
fprintf(fileID, 'format compact;\n');
fprintf(fileID, 'format long g;\n');
% Close the file.
fclose(fileID);
% Open the file in the editor.
edit(fullFileName);
This is much, much more convenient that having to remember and type some m-file name and pass it some function/file name.

Adam
Adam 2014-12-5
编辑:Adam 2014-12-5
What is the
a=[x '.m'] type a delete x.m
line of code trying to achieve?
I do something similar to this to create a template unit test function which I then open for further editing, but I just finish my creation of the file with:
open( myCreatedFilename );
and it opens the created file in the editor.
I don't understand from your code or question though whether you want your created file opened or not. You don't need an 'edit' instruction to create the file so if you don't want it open in the editor then just don't call 'edit'.

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

尚未输入任何标签。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by