1 %% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 %% This program is free software; you can redistribute it and/or modify it under
4 %% the terms of the GNU General Public License as published by the Free Software
5 %% Foundation; either version 3 of the License, or (at your option) any later
8 %% This program is distributed in the hope that it will be useful, but WITHOUT
9 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 %% You should have received a copy of the GNU General Public License along with
14 %% this program; if not, see <http://www.gnu.org/licenses/>.
17 %% @deftypefn {Function File} {[@var{fhandle}, @var{fullname}] = } data2fun (@var{ti}, @var{yi})
18 %% @deftypefnx {Function File} {[ @dots{} ] = } data2fun (@var{ti}, @var{yi},@var{property},@var{value})
19 %% Creates a vectorized function based on data samples using interpolation.
21 %% The values given in @var{yi} (N-by-k matrix) correspond to evaluations of the
22 %% function y(t) at the points @var{ti} (N-by-1 matrix).
23 %% The data is interpolated and the function handle to the generated interpolant
26 %% The function accepts property-value pairs described below.
30 %% Code is generated and .m file is created. The @var{value} contains the name
31 %% of the function. The returned function handle is a handle to that file. If
32 %% @var{value} is empty, then a name is automatically generated using
33 %% @code{tmpnam} and the file is created in the current directory. @var{value}
34 %% must not have an extension, since .m will be appended.
35 %% Numerical value used in the function are stored in a .mat file with the same
36 %% name as the function.
39 %% Type of interpolation. See @code{interp1}.
46 function [fhandle fullfname] = data2fun( t, y, varargin)
48 %% Check input arguments
49 interp_args = {"spline"};
50 given = struct("file",false);
53 interp_args = varargin;
55 opt_args = fieldnames (given);
56 [tf idx] = ismember( opt_args, varargin);
57 for i=1:numel(opt_args)
58 given.(opt_args{i}) = tf(i);
62 %% TODO: check that file will be in the path. Otherwise fhabdle(0) fails.
64 if !isempty(varargin{idx(1)+1})
66 [DIR fname] = fileparts(varargin{idx(1)+1});
70 [DIR fname] = fileparts (tmpnam (pwd (),"agen_"));
74 interp_args(idx(1)+[0 1]) = [];
77 if isempty(interp_args)
79 interp_args = {"spline"};
84 pp = interp1 (t, y, interp_args{end}, 'pp');
87 fullfname = fullfile (DIR,[fname ".m"]);
88 save("-binary",[fullfname(1:end-2) ".mat"],"pp");
90 bodystr = [" persistent pp\n" ...
91 " if isempty(pp)\n" ...
92 " pp = load([mfilename()" ' ".mat"' "]).pp;\n"...
94 " z = ppval(pp, x);"];
96 strfunc = generate_function_str(fname, {"z"}, {"x"}, bodystr);
98 fid = fopen ( fullfile (DIR,[fname ".m"]), "w");
99 fprintf (fid, "%s", strfunc);
102 disp(["Function generated: " fullfname ]);
103 fhandle = eval(["@" fname]);
107 fhandle = @(t_) ppval (pp, t_);
113 function str = generate_function_str(name, oargs, iargs, bodystr)
115 striargs = cell2mat ( cellfun (@(x) [x ", "], iargs, "UniformOutput", false));
116 striargs = striargs(1:end-2);
118 stroargs = cell2mat ( cellfun (@(x) [x ", "], oargs, "UniformOutput", false));
119 stroargs = stroargs(1:end-2);
121 if !isempty (stroargs)
122 str = ["function [" stroargs "] = " name "(" striargs ")\n\n" bodystr ...
125 str = ["function " name "(" striargs ")\n\n" bodystr ...
132 %! t = linspace(0,1,10);
133 %! y = t.^2 - 2*t + 1;
136 %! fhandle = data2fun(t,y);
137 %! assert(y,fhandle(t));
140 %! [fhandle fname] = data2fun(t,y,"file","testdata2fun");
141 %! yt = testdata2fun(t);
144 %! assert(y,fhandle(t));
147 %! delete([fname(1:end-2) ".mat"]);
150 %! [fhandle fname] = data2fun(t,y,"file","");
151 %! yt = testdata2fun(t);
154 %! assert(y,fhandle(t));
157 %! delete([fname(1:end-2) ".mat"]);
160 %! [fhandle fname] = data2fun(t,y,"file","testdata2fun","interp","linear");
161 %! yt = testdata2fun(t);
164 %! assert(y,fhandle(t));
167 %! delete([fname(1:end-2) ".mat"]);