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 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Mandar Patwardhan
2017년 5월 11일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!