# How to store vector of doubles in a single cell in a table?

24 views (last 30 days)
Ellen Maas on 28 Aug 2020
Answered: Ayush Gupta on 4 Sep 2020
Hi everyone,
I would like to store the results from a cfit object in a table after fitting a model with the fit function, including the type of model, the general form, and the coefficients. The coefficients are returned as a vector of doubles, and I would like to store them in the table. Different models can have different numbers of coefficients.
So one solution I'm pursuing is to put the vector of coefficients all into one cell in the table, with a "table" data type. This is the code I am testing with:
x_data = [1;2;4;5;3;7;8;8];
y_data = [12;18;18;20;24;22;25;28];
model_results_tbl = table('Size',[1,5],...
'VariableNames',{'x_label','y_label','fittype','model_form','model_coeff'},...
'VariableTypes',{'cellstr','cellstr','cellstr','cellstr','table'});
[poly1_result, poly1_gof, poly1_output] = fit(x_data,y_data,'poly1');
model_results_tbl(1,:) = {'Xdata','Ydata','poly1',formula(poly1_result),...
table(coeffvalues(poly1_result))};
The code works, except that the "model_coeff" cell is a 1x0 table with no data in it. Running the "table(coeffvalues(poly1_result))" code by itself gives me the output I expect, though:
>>table(coeffvalues(poly1_result))
ans =
table
Var1
________________
1.4903 13.796
What am I doing wrong?
Or is there a better way to do this?
Thanks.

Ayush Gupta on 4 Sep 2020
The following workaround can be used to store model coefficients in the table. Refer to the following code:
x_data = [1;2;4;5;3;7;8;8];
y_data = [12;18;18;20;24;22;25;28];
model_results_tbl = table('Size',[1,5],...
'VariableNames',{'x_label','y_label','fittype','model_form','model_coeff'},...
'VariableTypes',{'cellstr','cellstr','cellstr','cellstr','cell'});
[poly1_result, poly1_gof, poly1_output] = fit(x_data,y_data,'poly1');
coeff = coeffvalues(poly1_result);
model_results_tbl(1,:) = {'Xdata','Ydata','poly1',formula(poly1_result), mat2cell(coeff,1)};

### Categories

Find more on Tables in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!