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

13 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Ayush Gupta
Ayush Gupta il 4 Set 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)};

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by