altmany / export_fig

A MATLAB toolbox for exporting publication quality figures
BSD 3-Clause "New" or "Revised" License
1.27k stars 365 forks source link

append_pdf cell input issue? #299

Closed hhaugnes closed 4 years ago

hhaugnes commented 4 years ago

hi Yair, thank you for a great code library here, super helpful. I dont know if I am doing something wrong but I could not get the append_pdfs to take a cell array with multiple PDF files as input, it refuses to accept it, so I had to amend the append_pdfs function with the below.

When I call the function, it is like something like this: append_pdfs(savefilename,{'file1.pdf','file2.pdf})

This however returns an error as follows:

Error using fprintf
Function is not defined for 'cell' inputs.

Error in append_pdfs>prepareCmdFile (line 115)
    fprintf(fh, ' "%s"', varargin{:});

Error in append_pdfs (line 79)
    prepareCmdFile(cmdfile, output, varargin{:});

So I inserted the below code to make it work:

function append_pdfs(varargin)

    if nargin < 2,  return;  end  % sanity check

% EDIT START: convert to flat cell otherwise this doesnt work
    if nargin==2 && iscell(varargin{2})
        flatcell = {};
        for i = 1:length(varargin{2})
           flatcell = [flatcell varargin{2}(i)]; %flatten to a single row.
        end
        varargin=[varargin{1} flatcell];        
    end
% EDIT END

    % Are we appending or creating a new file
    append = exist(varargin{1}, 'file') == 2;

Just wanted to raise it in case helpful but could not find another way and my calling of the function seems consistent with the documentation. Although probably isnt:) Thanks as always for great matlab functions. SUper useful.

altmany commented 4 years ago

Fixed (in a slightly different way, but similar end-result). append_pdfs now accepts either a cell-array of char filenames, or an array of string filenames. String filenames are also now accepted as individual filename inputs, as well as regular char filenames.