Why I get Reference to a cleared variable x. in my code?

function data=surface_point(x,y)
clc, clear all, close all
px = [0 50 100 150 200 250 300; 0 50 100 150 200 250 300; 0 50 100 150 200 250 300; 0 50 100 150 200 250 300];
py = [0 0 0 0 0 0 0; 50 50 50 50 50 50 50;100 100 100 100 100 100 100;150 150 150 150 150 150 150];
pz = [-15 70 90 -60 -40 15 40; 40 -5 -20 -40 50 30 -20; 0 -45 -30 30 40 -10 -40; -30 5 50 0 -30 -20 20];
n=size(px,1)-1;
m=size(px,2)-1;
c=30;
u = x/300;
v = y/150;
for i=1:n+1
for j=1:m+1
sx=sx+px(i,j)*B(i-1,n,u)*B(j-1,m,v);
sy=sy+py(i,j)*B(i-1,n,u)*B(j-1,m,v);
sz=sz+pz(i,j)*B(i-1,n,u)*B(j-1,m,v);
end
end
data = [sx sy sz];

2 个评论

"Why I get Reference to a cleared variable x. in my code?"
Because for no reason at all you put
clc, clear all, close all
at the start of your function. Why use some commands in your function if you don't know what they are doing? How are these commands in any way related to the functionality of what you are writing?
He probably wrote this without a function wrapper and forgot to remove that line before adding the function definition, having decided to wrap that code in a call.

请先登录,再进行评论。

 采纳的回答

Because, although you pass in x and y, you delete them immediately with the command
clear all
Don't do that!!! If you don't do that, x will still be there when you need to use it.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by