Creating a matlab shortcut to create a matlab script file with the assignment name and a certain header.

Hi Everyone,
I have been browsing the community and wasn't able to find an answer to my question so I figured I would post it. I am trying to create a matlab shortcut so that I don't need to know the location of my template everytime I need to create a script for an assignment or research code. I have a set header that I like to use. The issue is that after the input request for the assignment name I want to open a file with that name which is accomplished with the edit command but then I can't seem to get the fileid for it in order to use that with fprintf to insert the header into the new matlab script. Ideally I would like to be able to take the name inputted by the user remove the underscores with strrep replacing them with spaces and inputting this name into the title part of the header. Below is my current shortcut which is not working.
s=input('What is the name of this assignment? (No Spaces, use underscores): ','s');
edit(s)
close all
fprintf(s,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n');
fprintf(fid,'% %%\n');
fprintf(fid,'% Title: %%\n');
fprintf(fid,'% Created By: Paul Stockett %%\n');
fprintf(fid,'% Contributors: None %%\n');
fprintf(fid,'% Date of Creation: MM/DD/YYYY %%\n');
fprintf(fid,'% Date of Last Modification: MM/DD/YYYY %%\n');
fprintf(fid,'% %%\n');
fprintf(fid,'% Purpose: To perform the calculations necessary to complete %%\n');
fprintf(fid,'% Assignment for Class %%\n');
fprintf(fid,'% %%\n');
fprintf(fid,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n');
fprintf(fid,'\n %Preparing Workspace\n')
fprintf(fid,'clear all\n')
fprintf(fid,'close all\n')
fprintf(fid,'clc all\n')
fprintf(fid,'\n %Fundamental Constants\n')
Thanks in advance!

Answers (1)

insert this before the first fprintf
fid = fopen(s, 'wt') ;
if fid == -1
return
end
and end your script with
fclose(fid)
edit (s)

3 Comments

When I do this, it is generating a blank file named s but it is not a matlab script so it will not allow me to run it. It is also only printing some of the header. This is what it is printing in the s file:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc all
to print a single % you need to use %%.
fprintf(fid,'%%') % prints a single % in the file
Thanks for that! It is still writing it into a non descript file type instead of the .m file that is created with the edit command. Is there anyway to get it to write into the .m file instead?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 6 Feb 2018

Commented:

on 9 Feb 2018

Community Treasure Hunt

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

Start Hunting!