MATLAB: Saving an interpolated function to an m-file

functionsinterpolationMATLABsave

I have some complicated function that I have interpolated using interp1() so that evaluating it is much faster. I would like the result of this interpolation to be available systemwide by saving it as a function in an .m-file. An example:
xd = -1:0.01:1;
yd = xd.^2;
f = @(x) interp(xd, yd, x);
Is there a way to save the result f to f.m such that I can evaluate f(x) anywhere?

Best Answer

Hi,
To my understanding, you want to save the custom interpolation as function to be available for further evaluation.
For that you can write the code as a MATLAB function.
Here is a sample version for the example code you provided
function res = f(x)
xd = -1:0.01:1;
yd = xd.^2;
res = interp(xd, yd, x);
end
Hope this helps.