Creating a matlab shortcut to create a matlab script file with the assignment name and a certain header.
Show older comments
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)
Jos (10584)
on 6 Feb 2018
Edited: Jos (10584)
on 6 Feb 2018
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
Paul_s
on 7 Feb 2018
Jos (10584)
on 7 Feb 2018
to print a single % you need to use %%.
fprintf(fid,'%%') % prints a single % in the file
Paul_s
on 9 Feb 2018
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!