How to read matrix data from excel?

6 views (last 30 days)
Eddy Iswardi on 13 Mar 2020
Commented: Bob Thompson on 13 Mar 2020
.My excel data is above. I want to change my 'x' and 'y' in my code with importing data from excel. How to make the code. This my latest code
clc;
clear all;
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0];
y=[190 187 188 189 190 190 190 190 190 190 192 193 193 193 193 196 196 197 198 200 196 197 197 198 196 192 194 194 193 193 195 196 196 199 197 196 195 196 195 196 196 196 195 194 193 188 190 190 193 190 188 186 185 184 182 185 184 183 183 181 184 185 185 184 185 185 188 189 190 189 187 187 188 188 189 189 188 186 183 176 174 181 181 177 175 175 173 176 178 177 174 174 174 173 173 174 174 178 178 180];
n =length(x);
my=sum(y);
m = sum(bsxfun(@power,x(:),1:(2*n)),1);
xv = x(1:n);
yv = y(1:n);
ny = sum(bsxfun(@times,bsxfun(@power,xv(:),1:99),yv(:)),1);
for na=2:(n-1)
for i=1:na
for j=1:na
for k=1:na
A(1,1)=n;
A(i,1)=m(i);
if k>1
A(i,k)=m(k);
if A(i,k)==A(i,k-1)
A(i,k)=m(k+1);
elseif A(i,k)<A(i,k-1)
A(i,k)=m(i+k-1);
end
end
end
end
end
B=[my 1];
for i=2:na
b = ny(i);
B(i)=b;
end
for k=1:na-1
for i=k+1:na
if A(i,k)~=0
lambda=A(i,k)/A(k,k);
for j=1:na
A(i,j)=A(i,j)-lambda*A(k,j);
end
B(i)=B(i)-lambda*B(k);
end
end
end
for i=na:-1:1
sum=0;
X(i)=0;
for j=1:na
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
for c = 1:n
R(c) = 0;
for d = 1:na
R(c) = R(c)+X(d)*x(c)^(d-1);
end
end
na
R
error(na)=(y-R)/R
figure(na)
plot(x,R,x,y,'-');
legend('Ymeasured','Orde')
hold on
end
Someone give me solution, that I shoul change my 'x' and 'y' code like this
x = T.x;
y = T.y;
But if I use that code, I will get an error like in picture. How to solve this?
Bob Thompson on 13 Mar 2020
The readtable method they suggested is certainly one way of reading data from an excel document. You can also use xlsread, or readmatrix if your version of matlab is new enough. You will need to make sure you are indexing things properly, but the actual importing should be fairly simple.