MATLAB Correcting Axis Labeling in MATLAB Function | Need Help

  • Thread starter Thread starter forumfann
  • Start date Start date
  • Tags Tags
    Axis
AI Thread Summary
The discussion centers around a MATLAB function that incorrectly interchanges the x-axis and y-axis in a surface plot. A user seeks assistance to correct this issue. The solution provided suggests two approaches: modifying the loop to set z(i,j) = j instead of i for a different orientation, or simply swapping the labels by changing ylabel and xlabel. Additionally, a more efficient MATLAB coding method is recommended using meshgrid to create x and y matrices, streamlining the code for better readability and functionality.
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
 

Similar threads

Replies
8
Views
2K
Replies
10
Views
3K
Replies
4
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
2
Views
3K
Back
Top