No BSD License  

Highlights from
compute_if_doesnt_exist

from compute_if_doesnt_exist by Joao Carreira
Computes your function if its output doesn't exist already in file

compute_if_doesnt_exist(the_func, the_inp, file, force_compute)
%function the_output = compute_if_doesnt_exist(the_func, the_inp, file, force_compute)
% INPUTS: 
% - the_func: a function handle
% - the_inp : cell array with input variables
% - file    : the name of the file where to save the_output
% - force_compute: optional argument, that forces computation of the_func
%
% Output:
% - the_output: the result of the computation, or the load()
%
% Comments:
% - only accepts one output, but you can always put everything into a
% structure.
%
% Example:
% 
%  the_func = @dir;
%  the_inp = './';
%  file = 'benfica_is_the_greatest';
%  data = compute_if_doesnt_exist(the_func, the_inp, file);
% 
% João L. Carreira (joaoluis at isr.uc.pt) 
% May 2008

function the_output = compute_if_doesnt_exist(the_func, the_inp, file, force_compute)
  if (~exist(file, 'file') || ((nargin == 4) && (force_compute == true)))
    the_input = transform_cell_into_string(the_inp);
    the_output = eval(['the_func(' the_input ')']);
    save(file,'the_output');
  else
    load(file);
    if(~exist('the_output', 'var'))
      error([file ' was loaded but it wasn''t generated by this function!!'...
            'Delete it and try again.'])
    end
  end

  function the_string = transform_cell_into_string(the_input);
    n_strings = length(the_input);
    the_string = '';
    for i=1:n_strings 
      the_string = [the_string 'the_inp{' int2str(i) '}'];
      if(i < n_strings)
        the_string = [the_string ','];
      end
    end

    

Contact us at files@mathworks.com