Is it possible to extract a comment line from the script itself?

2 ビュー (過去 30 日間)
Mr M.
Mr M. 2015 年 5 月 19 日
回答済み: Star Strider 2015 年 5 月 19 日
I would like to write the first comment line as a title on my plot.

採用された回答

Star Strider
Star Strider 2015 年 5 月 19 日
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.

その他の回答 (1 件)

Joseph Cheng
Joseph Cheng 2015 年 5 月 19 日
why would you want to do this and not just declare the first comment as a string? if it is truly the first line of the script just
fid = fopen('yourscript.m');
titleline = fgelt(fid);
fclose(fid)
then use title line. if it isn't then just declare the comment as a variable of type string. or copy it.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by