[Ipe-discuss] How to Import Figure from Matlab to Ipe

Tim Hutt tdhutt at gmail.com
Tue Dec 2 11:47:13 CET 2008


This is the easiest way to save a figure as an ipe-editable PDF. Save the
following three matlab functions in your path. Then read them. You need
pdfcrop.pl and ActivePerl (if using windows) to crop the PDFs. Doing
SaveFigure('fig.pdf'); will save a cropped PDF that IPE can edit. You can
un-crop it by removing the unnamed stylesheets in IPE (although this did
crash IPE just now). Or you can comment out the CropPDF line.

---------8<---------

function SaveFigure(filename, figure_handle)
% Save the figure to an ipe-compatible PDF.
% Filename is the name of the file to save it to (you can omit the .pdf
% extension if you like). figure_handle gives the figure to save. If
% omitted the current figure is used.

filename(find(filename == '\')) = '/';

if numel(filename >= 4) && strcmpi(filename(end-3:end), '.pdf')
    filename = filename(1:end-4);
end

if nargin < 2
    figure_handle = gcf;
end

saveas(figure_handle, filename, 'pdf');
CropPDF(filename);
PDFtoIPE(filename);

End


function CropPDF(pdffile)
% Uses the pdfcrop program to crop the PDF.
% pdffile is the file name (without the extension) to crop. It is
% overwritten.

% You need the pdfcrop.pl script in the current directory (or you can
% modify the command line below to point to it), and a version of perl
% installed.
%
% pdfcrop.pl is available on ctan, and you can use ActivePerl on windows.

system(['perl pdfcrop.pl --verbose --margin 5 "' pdffile '.pdf"']);
movefile([pdffile '-crop.pdf'], [pdffile '.pdf']);

End

function PDFtoIPE(pdffile)
% Uses the PDFtoIPE program to convert the PDF to one IPE can edit.
% pdffile is the file name (without the extension) to convert. It is
% overwritten.

% Alter these paths to point to the location of your ipe programs.
pdftoipe = 'C:\Program Files\ipe-6.0pre30\bin\pdftoipe.exe';
ipetoipe = 'C:\Program Files\ipe-6.0pre30\bin\ipetoipe.exe';

system(['"' pdftoipe '" "' pdffile '.pdf" "' pdffile '.xml"']);
system(['"' ipetoipe '" -pdf "' pdffile '.xml" "' pdffile '.pdf"']);
delete([pdffile '.xml']);

End

---------8<--------

Hope this helps.

-Tim




More information about the Ipe-discuss mailing list