How to export the result of the function fit to a file?

74 Ansichten (letzte 30 Tage)
Wong Slocked
Wong Slocked am 16 Apr. 2020
Kommentiert: dpb am 17 Apr. 2020
1.After I use the function fit like this,
p=fit([x,y],z,g,op);
I get a result p, data type is sfit.
I read the document, and found out that p can be invoked just like a function:
z=p(x,y);
But when I tried to write this function to a file like this,
matlabFunction(p,'file','p');
I received an error saying that matlabFunction cannot write this function to a file.
2. So I came up with another way.
I noticed the result is structured as
General model:
val(x,y) = p9.*x.^3+p8.*x.^2.*y+p7.*x.*y.^2+p6.*y.^3
+p5.*x.^2+p4.*x.*y+p3.*y.^2+p2.*x+p1.*y+p0
Coefficients (with 95% confidence bounds):
p1 = -0.002069 (-0.002113, -0.002025)
p2 = -0.003244 (-0.003276, -0.003213)
p3 = 0.03401 (0.03396, 0.03405)
p4 = 0.0003719 (0.000349, 0.0003948)
p5 = -0.01642 (-0.01644, -0.0164)
p6 = -0.0005034 (-0.0005421, -0.0004647)
p7 = 0.0002566 (0.0002348, 0.0002784)
p8 = 0.004362 (0.004347, 0.004376)
p9 = 0.0002652 (0.0002521, 0.0002784)
p0 = -0.01646 (-0.01649, -0.01643)
I thought it's ok that I export the coefficients in this way:
coef(1)=p.p1;
coef(2)=p.p2;
But in my project, when the number of coefficients reaches larger, and I want to do this with a for loop:
for i=1:n
coef(i)=p.p(i);
end
I found that matlab doesn't support the syntax p.p(i)!
So how can I export the result of the function fit to a file anyway? I'm feeling a little upset.

Akzeptierte Antwort

dpb
dpb am 16 Apr. 2020
Bearbeitet: dpb am 16 Apr. 2020
It's not a symbolic object, nor a function--it's a cfit object. It has associated methods and the default method is to evaluate the function with the input arguments so it does superficially look like a function. In this case, however, just because it looks and quacks like, it isn't a duck. :)
The simplest way to output to a file for later reuse is
save filename objectname
This has the distinct advantage that you regain the entire functionality of the object on recall instead of just pieces...
>> p=fit([1:10].',rand(10,1),'poly1')
p =
Linear model Poly1:
ans(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = 0.003135 (-0.06473, 0.071)
p2 = 0.4877 (0.06662, 0.9088)
>> whos p
Name Size Bytes Class Attributes
p 1x1 708 cfit
>> save pfit p
>> whos -file pfit
Name Size Bytes Class Attributes
p 1x1 708 cfit
>> clear p
>> load pfit
>> p
p =
Linear model Poly1:
p(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = 0.003135 (-0.06473, 0.071)
p2 = 0.4877 (0.06662, 0.9088)
>>
BTW, need to read the documentation on using the fit objects carefully...and the tab completion at the command line is very helpful in prompting for methods and the error messages if try wrong things are generally good in leading to the right answer. eg, for the above to retrieve coefficients, use:
>> p.coeffvalues
Error using cfit/subsref (line 18)
The name 'coeffvalues' is not a coefficient or a problem parameter. You can only use dot notation to access the coefficients and problem
parameters of a cfit or sfit, e.g., 'f.p1'.
For the current fit, you can access these properties: p1, p2
You can get coefficient names and values either by name, e.g., p1 = f.p1, or by using the coeffnames or coeffvalues functions, e.g., names =
coeffnames(f).
To use methods, use functional notation instead, e.g., plot(f).
>> coeffvalues(p)
ans =
0.0031 0.4877
>>
So, again, you don't need loops; you could export the array of coefficients, but by doing so you'll have them but have lost the facility to use them later unless you code to evaluate the function explicitly.
  2 Kommentare
Wong Slocked
Wong Slocked am 17 Apr. 2020
Thank you! The problem has been resolved.
I do need to read the document more carefully, I really didn't manage to find the function coeffvalues or the function save in the document of fit(maybe they appear in some related docs, I need to pay more attention to related docs).
It's true that I read the error messages carelessly before. Thanks again for your tip and I will correct my bad habits in programing in matlab.
dpb
dpb am 17 Apr. 2020
Output Arguments
fitobject — Fit result
cfit | sfit
Fit result, returned as a cfit (for curves) or sfit (for surfaces) object. See Fit Postprocessing for functions for plotting, evaluating, calculating confidence intervals, integrating, differentiating, or modifying your fit object.
save is unrelated to fit; it is the generic tool for any variable(s) @ https://www.mathworks.com/help/matlab/ref/save.html

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by