In order to automate the fitlm command I need to select numerical data from a matrix, and matching variable names from another character array. I get an error on fitlm command
5 次查看(过去 30 天)
显示 更早的评论
% Test script which shows the problem
% Input Data correctly entered in a table
% X and Y are number arrays as shown in table below
X=[114.6986
130.8867
137.2275
158.3874
164.9807
150.4959
153.5719
162.3315
150.4818
150.6757
126.0978
146.8826
108.8821
140.6874
141.9079
141.3292
147.5428
178.0340
147.4099
146.6944
149.0788
136.2100
120.1847
139.2640
130.8207
151.0124
126.2979
128.7861
150.1999
145.1715
124.0876
162.7852
150.6057
110.8681
127.9843
141.6658
135.8723
140.0876
160.0989
124.4767];
Y= [90.1289
113.7593
132.9390
122.6130
136.6224
123.4329
126.8783
157.6137
145.6993
129.0664
111.1469
138.5979
97.5274
134.1083
124.0515
120.4644
140.7517
170.4020
122.8751
127.7062
133.2769
107.5278
119.6132
111.1775
122.9596
143.7457
113.7101
110.3727
132.7626
119.9252
118.1571
149.1560
134.6609
90.6230
108.7946
135.4210
122.6799
103.4755
140.5454
95.2746];
% Variable names have embedded spaces. Example below!
VarNames(4,1:15)='Data 1 '
VarNames(7,1:15)='Data 2 '
% Produce the Table
tbl=table(X,Y,'VariableNames',{erase(strtrim(VarNames(4,:))," "),erase(strtrim(VarNames(7,:))," ")})
% Fit linear regression line
lm=fitlm(tbl,'erase(strtrim(VarNames(OriColumns(i,1),:))," ") ~ erase(strtrim(VarNames(OriColumns(i,2),:))," ")')
fignum=fignum+1;
figure(fignum)
orient Landscape;
set(gcf,'units', 'normalized', 'outerposition', [0 0 1 1]);
plot(lm)
0 个评论
回答(3 个)
Cris LaPierre
2023-5-16
You haven't share what the values of OriColumns and i are, but either way, you have a character array. It is no longer an executable command. It's just letters and words.
My recommendation would be to capture the result of your expression in a variable before you call fitlm, and pass that variable in as the 2nd input to fitlm.
% Test script which shows the problem
% Input Data correctly entered in a table
% X and Y are number arrays as shown in table below
X=[114.6986
130.8867
137.2275
158.3874
164.9807
150.4959
153.5719
162.3315
150.4818
150.6757
126.0978
146.8826
108.8821
140.6874
141.9079
141.3292
147.5428
178.0340
147.4099
146.6944
149.0788
136.2100
120.1847
139.2640
130.8207
151.0124
126.2979
128.7861
150.1999
145.1715
124.0876
162.7852
150.6057
110.8681
127.9843
141.6658
135.8723
140.0876
160.0989
124.4767];
Y= [90.1289
113.7593
132.9390
122.6130
136.6224
123.4329
126.8783
157.6137
145.6993
129.0664
111.1469
138.5979
97.5274
134.1083
124.0515
120.4644
140.7517
170.4020
122.8751
127.7062
133.2769
107.5278
119.6132
111.1775
122.9596
143.7457
113.7101
110.3727
132.7626
119.9252
118.1571
149.1560
134.6609
90.6230
108.7946
135.4210
122.6799
103.4755
140.5454
95.2746];
% Variable names have embedded spaces. Example below!
VarNames(4,1:15)='Data 1 '
VarNames(7,1:15)='Data 2 '
% Produce the Table
tbl=table(X,Y,'VariableNames',{erase(strtrim(VarNames(4,:))," "),erase(strtrim(VarNames(7,:))," ")})
% Fit linear regression line
i = 1;
OriColumns(1,1) = 4;
OriColumns(1,2) = 7;
modelSpec = erase(strtrim(VarNames(OriColumns(i,1),:))," ") + " ~ " + erase(strtrim(VarNames(OriColumns(i,2),:))," ");
lm=fitlm(tbl,modelSpec)
fignum=1;
figure(fignum)
orient Landscape;
set(gcf,'units', 'normalized', 'outerposition', [0 0 1 1]);
plot(lm)
0 个评论
the cyclist
2023-5-16
One way that is close to the syntax you used is this:
lm=fitlm(tbl,sprintf('%s ~ %s',erase(strtrim(VarNames(OriColumns(i,1),:))," "),erase(strtrim(VarNames(OriColumns(i,2),:))," ")))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Analysis of Variance and Covariance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!