MATLAB Answers

How to read excel data in matlab code

1 view (last 30 days)
Eddy Iswardi
Eddy Iswardi on 10 Mar 2020
Commented: Eddy Iswardi on 11 Mar 2020
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
And this is my excel data

Answers (1)

Walter Roberson
Walter Roberson on 10 Mar 2020
T = readtable('polinom.xlsx');
x = T.x;
y = T.y;

Community Treasure Hunt

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

Start Hunting!