MATLAB: Can someone help me with the code? It giving me an error in line 2 saying that it doesn’t exist or it inst able to enter it in as x.

function y = bubble( x )
n = length(x); %This is where the error occurs
sort= 0;
k = 0;
while ~ sort
sort =1;
k = k+1;
for j = 1:n-k
if x(j)>x(j+1)
temp = x(j);
x(j)=x(j+1);
x(j+1) = temp;
sorted = 0;
end
end
end
p=x

Best Answer

Show us how you're calling the function and what you're passing in for x. Like in your main program do this
% define x
x = ...... whatever you do
whos x % Let's see what it is
y = bubble( x )
Then come back here and paste the results of the "whos" command that will show up in the command window.