MATLAB: Is it possible to extract a comment line from the script itself
commentplotprintscripttext;
I would like to write the first comment line as a title on my plot.
Best Answer
Try this:
fidi = fopen('scriptfilename.m','r');
comloc1 = 0;
while ~comloc1
t = fgets(fidi);
comloc1 = strfind(t,'%');
end
Comment1 = t(comloc1(1),:);
with‘scriptfilename.m’ being the name of the script you are running. You are reading it as a text file.
The‘Comment1’ assignment pulls out the entire comment with the‘%’, so I leave that to you to deal with as you wish, since I don’t know what your code looks like.
If you are unable to modify that source, then consider the diary command, or consider executing that source within evalc() and writing out the characters you get back from evalc().
Much better would be to follow IA's suggestion of writing to a file within the loop itself. fopen(), fprintf(), eventually fclose()
Best Answer