Correcting Axis Labeling in MATLAB Function | Need Help

  • Context: MATLAB 
  • Thread starter Thread starter forumfann
  • Start date Start date
  • Tags Tags
    Axis
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
forumfann
Messages
24
Reaction score
0
The x-axis and y-axis are interchanged in the figure by running the following MATLAB function I wrote.

Could anyone help me to get it corrected? Thanks in advance for any helpful answer.

function axislabeling(n)
x=1:1:n;
y=1:1:n;

z=zeros(n,n);

for i=1:n
for j=1:n
z(i,j)=i;
end
end
surf(x,y,z(x,y))

ylabel('y-axis')
xlabel('x-axis')
zlabel('z-axis')
colorbar
 
Physics news on Phys.org
Do you mean you want the function to slant the other way? Just put z(i,j) = j in you loop instead of i. If you simply want to change the labeling, put ylabel('x-axis') and xlabel('y-axis'). Also, a more "Matlab" way of writing that code would be:

[x y] = meshgrid(1:n);
z = x;

figure
surf(x, y, z)
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
colorbar