# How to extract multiple cfit or fit values into one table

6 views (last 30 days)
B on 16 Dec 2020
Commented: B on 17 Dec 2020
Dear all,
So I have a 31x 19 matrix data and I had to fit the first column data (31X1) and the rest of all the 18 columns (31x18).
I wrote a simple if function to do this but my challenge now is about how to extract each fit coefficients (f) including confidence intervals into one Table. I already used coeffvalues but only returns my last f as the ans
Mycode is a follows
r = randi([0, 100], 31,19)
b = (r(:,1));b2=b(:,2:end);
for i= 1:size(b2,2)
% [f, gof_info] = fit(b1,b(:,2),'power1');
f{i}= fit(b, b2(:,i),'power1')
%f_values= coeffvalues(f{i})
end
Your help will be deeply appreciated.
Thanks
E

Isabelle Levy on 17 Dec 2020
Hi E,
I understand you’re having trouble condensing data that you’ve generated using the fit function. I noticed a few potential typos in your code. See my comments in green:
r = randi([0, 100], 31,19) %Power functions are unable to fit to data where X may contain nonpositive values; change the range from [0-100] to [1-100]
b = (r(:,1));
b2=b(:,2:end); %I think you meant to write b2 = r(:,2:end)
for i= 1:size(b2,2)
f{i}= fit(b, b2(:,i),'power1') %The appropriate syntax for a fit function would be f = fit(b, b2(:,i),'power1')
end
In addition to the coeffvalues function, you will also want to use the confint function to obtain the confidence levels associated with each result. With that being said, consider the revised following revised code (see comment below). The first iteration of the for loop populates the first two columns of an array with the corresponding coefficients and confidence intervals determined by fitting the data. The for loop executes a total of 18 times, so the result is a 3-by-36 array where the first row stores the coefficients and the second two rows store the corresponding lower and upper bounds. The array2table function then converts the array to a table.
##### 2 CommentsShowHide 1 older comment
B on 17 Dec 2020
Good answer, I guess for the start:)

### Categories

Find more on Fit Postprocessing in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!