I am trying to write data from fitlm model to excel and write a column vector of the name of variables into my excel table

4 views (last 30 days)
Hi,
Here is my code:
clear all
X = xlsread('example.xlsx', 1, 'A2:A857'); %get data from column in excel
Y = xlsread('example.xlsx', 1, 'B2:B857');%get data from column in excel
md1 = fitlm(X, Y); %run correlation test between different variables
T = table([''; 'Intercept'; 'Y']); %create column vector of compared variable names
writetable(T, 'example.xlsx', 'Sheet', 2, 'Range', 'A1'); %add names to spreadsheet
writetable(md1.Coefficients, 'example.xlsx', 'Sheet', 2, 'Range', 'B1'); %add results to spreadsheet
writetable(md1.Rsquared.Ordinary, 'example.xlsx', 'Sheet', 2, 'Range', 'G1'); %add results to spreadsheet
writetable(md1.Rsquared.Adjusted, 'example.xlsx', 'Sheet', 2, 'Range', 'G1'); %add results to spreadsheet
I have two problems:
1. I am returned the following error when trying to create a column vector to add to my spreadsheet's table:
Error using (line 8) Dimensions of matrices being concatenated are not consistent.
2. My second error is when using dot notation to collect the data from md1:
|Undefined function 'write' for input arguments of type 'double'.
Error in writetable (line 121) write(a,filename,varargin{:})
Error in (line 13) writetable(md1.Rsquared.Ordinary, 'example.xlsx', 'Sheet', 2, 'Range', 'F1'); %add results to spreadsheet |
Could you help me solve these problems, please?

Accepted Answer

Mandar Patwardhan
Mandar Patwardhan on 11 May 2017
1. To resolve the first problem, you need to correct the syntax of the table command as follows:
clear all
X = xlsread('example.xlsx', 1, 'A2:A857'); %get data from column in excel
Y = xlsread('example.xlsx', 1, 'B2:B857');%get data from column in excel
tbl = table(X,Y,'VariableNames',{'Intercept','Y'});
Also, refer to the documentation of the fitlm function which discusses an example similar to what you are trying to achieve. Fir Linear Regression Using Data in Table
2. For resolving the second problem, take a look at the documentation of the function writetable.
As the documentation suggests, the first argument to this function should be of the type "Table". If you provide md1.Rsquared.Ordinary (or Adjusted), which is a double, it will result in an error.

More Answers (0)

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!