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

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

 Accepted Answer

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 Comments

Thank you so much! Your code is working the way I wanted. Kindly educate me on these questions.
What is command syntax and what is function syntax.
I said funciton mcreate(x). And you said function mcreate(scriptname). Except the meanigful name of the input argument both are same.
I was trying to do these things:
With edit open a editor window with filename passed by the argumnet x.
After that I was trying to open the file with fid and write the lines I wanted in it.
It was being done but additinally it was also opening another window with x.m which I did not want. Using a I was trying to build the file name x.m and then delete it. I was also trying to type the newly created file using type command. If you can reason the observed great, it will be very useful.
Click on the link in my answer to see matlab's documentation on command syntax vs function syntax.
edit x
is command syntax. It edits the file called 'x'
edit(x)
is function syntax. It edits the file whose name is in variable x
Same issue with type and delete in your original code.
Got it, now! Thanks.
I will investigate further what difference it actually makes in execution and come back to you!. Thanks once again.
You didn't like the simplicity of creating a shortcut button like I suggested? I think it's easier because you don't have to remember the name of the script you made, and you don't have to then type in its name . You simply click a button and type in the name of your new m-file. But whatever you like....
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.
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.

Sign in to comment.

More Answers (2)

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.
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'.

Categories

Find more on Programming in Help Center and File Exchange

Products

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!