1

I have one .m file per plot and want to see in my draft printouts, which file was used to create it.

This should be done with a function which can be placed in my .m file and commented out, for the final version.

% addWatermarkFilename() % 

So far I found mfilename(), but it could not get the name of the calling function. I am looking also for a good way to put the text in the picture without changing the size.

Solution: I combined the suggestions by Luis Mendo and NKN to:

function [ output_args ] = watermarkfilename( )
% WATERMARKFILENAME prints the filename of the calling script in the
% current plot

s = dbstack; 
fnames = s(2).name;

TH = text(0,0,fnames,'Interpreter','none');
    TH.Color = [0.7 0.7 0.7];
    TH.FontSize = 14;
    TH.Rotation = 45;

uistack(TH,'bottom');    
end
Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
  • 2
    If you want to get the name of the function or script that called the current function or script, use `dbstack` as follows: `s = dbstack; callerName = s(2).name;` – Luis Mendo Mar 23 '17 at 14:47
  • 1
    @LuisMendo I am thankful for both Luis's and NKN's hints, but I think the `s(2).name` was the "key response" which I would like to mark as answer. – Jonas Stein Mar 23 '17 at 21:25
  • I posted it as an answer – Luis Mendo Mar 23 '17 at 22:02

2 Answers2

4

I suggest instead of commenting out a function from the code, use a proper flag. For instance the code can be something like this:

clc,clear,close all
addWaterMark = true;    % if true you will get the filename in the title
fname = mfilename;      % get file name mfilename('fullpath') for the full-path
t=-2*pi:0.1:2*pi;
y = sin(t);
plot(t,y); grid on;
xlabel('t');ylabel('y');
if addWaterMark
    title(['filename: ' fname '.m']);
else
    title('plot no.1');
end

enter image description here

A little bit playing with the text function, you can make a proper watermark. something like this:

clc,clear,close all
addWaterMark = true;
fname = mfilename;
fnames = [fname '.m'];
t=-2*pi:0.1:2*pi;
y = sin(t);
plot(t,y); grid on;
xlabel('t');ylabel('y');
if addWaterMark
    title(['filename: ' fnames]);
    t = text(-3,-0.4,fnames);
    t.Color = [0.7 0.7 0.7];
    t.FontSize = 40;
    t.Rotation = 45;
else
    title('plot no.1');
end

enter image description here

Note: Obviously the code between the if and else can be stored as a function that receives the string (char) from the fnames variable and a handle for the figure.

NKN
  • 6,482
  • 6
  • 36
  • 55
  • 1
    You can also flip the order of the axes children to get the text rendered bottommost (e.g. `ax = gca; ax.Children = flipud(ax.Children);`) – sco1 Mar 23 '17 at 16:01
  • 1
    @excaza instead of flipping, one can put a single object to bottom. I use this for lines: `LHx = line([1 2], [3 4]); uistack(LHx,'bottom');` – Jonas Stein Mar 23 '17 at 20:44
1

If you want to get the name of the function or script that called the current function or script, use dbstack as follows:

s = dbstack; % get struct with function call stack information 
callerName = s(2).name; % get name of second function in the stack

The first entry in s refers to the current function; the second refers to the one that called it.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147