I haven't been able to do this when originally saving the file, but do have a code that edits the header after saving. Thanks to Kelly Kearney for this answer which helped me create it.
function f = EditEPSHeader(FigFile,string)
% This function edits an eps file, [FigFile] to include [string] in the line of the header that contains "creator".
% Use LineNumInfo = dbstack; LineNum = LineNumInfo.line; and ThisScript = mfilename('fullpath'); to generate string.
% Emily Townsend November 2021
Key = '%%Creator:';
%%
% Read entire file as character array
FileContents = fileread(FigFile);
% Split into lines
FileContents = regexp(FileContents, '\r\n', 'split');
% Find lines starting with certain letters
istarget = startsWith(FileContents, Key) | ...
contains(FileContents, 'Creator');
% Replace lines that contain creator to include script name
for ii = find(istarget)
%FileContents{ii} = sprintf('%s : %s Line %d',FileContents{ii}, ThisScript, LineNum.line);
FileContents{ii} = sprintf('%s : %s',FileContents{ii}, string);
end
% Write to new file
fid = fopen(FigFile, 'wt');
fprintf(fid, '%s\n', FileContents{:});
fclose(fid);
f = sprintf('%s edited to include %s',FigFile, string);
end
Called with:
% TestEditEPSHeaderFunction.m
% This code plots something, saves it as eps, then edits the eps file to
% include the name of this code.
x = 1:200;
y = x.^2;
FH = figure;
plot(y,x,'o:');
title('a plot');
FigFile = 'TestFig.eps'
saveas(FH,FigFile,'epsc');
LineNum = dbstack;
ThisScript = mfilename('fullpath')
string = sprintf('%s Line %d', ThisScript, LineNum.line - 1);
f=EditEPSHeader(FigFile,string)