Add a comment to an eps file created with saveas

3 visualizzazioni (ultimi 30 giorni)
Emily
Emily il 23 Mar 2021
Commentato: Emily il 29 Nov 2021
I have many scripts that read my data, plot it, and save the figure as an encapsulated postscript file (used to use export_fig from the file exchange, now am using saveas). Sometimes I want to recreate a figure, and have to figure out which script generated it. Is there a way for my script to programmatically add a comment to the preamble of the postscript file so I can leave myself clues for recreating it? The head of my .eps file looks like this:
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: (MATLAB, The Mathworks, Inc. Version 9.6.0.1072779 \(R2019a\). Operating System: Linux)
%%Title: /home/etownsen/quantumplasmonics/Record/Disorder/2QE1by12w6/EnergyInitBasis2872QEsNotResw1stPl1by12w6Coul1tb-1EQE112EQE212ydHndCDa0.01Db0.3Lhop0.2Seed4.eps
%%CreationDate: 2021-03-10T10:44:35
%%Pages: (atend)
%%BoundingBox: 85 28 861 514
%%LanguageLevel: 3
%%EndComments
So it seems likely that adding info to the "Creator" line would not hurt the eps file, and probably I can even safely add a line like
%%Script: /here/something.m
Ideally it gets written when I create the file, but it might also be okay to add it after creating. I'd like the comment to contain the name (and path) of the script that created the eps. (And maybe the line number? but that's getting greedy.)
  2 Commenti
Emily
Emily il 23 Nov 2021
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)
Emily
Emily il 29 Nov 2021
This solution works on the windows machine. On the linux machine splitting into lines is accomplished by
FileContents = regexp(FileContents, '\n', 'split');
(the search term is just '\n' rather than '\r\n'.)

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Scripts in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by