Color map plotting, Error using surf() Data dimensions must agree
Show older comments
I am trying to make a colour map plot using the following code:
[x,z] = meshgrid(0:100:600,0:100:600);
mm = size(z);
for z2 = 1:3
for x2 = 1:3
E(z2,x2) = (x(x2))^2 + (z(z2))^2;
end
end
figure;
surf(x,z,E,'EdgeColor','None');
view(2);
xlabel('x','fontsize',20);
ylabel('z','fontsize',20);
colormap jet
I used z2 and x2 in the for loop because I only want to use the first to third element in the meshgrid, which means, 0,100,200 for both x and z, but matlab returns me the error:
Error using surf (line 74) Data dimensions must agree.
Error in testgraph (line 10) surf(x,z,E,'EdgeColor','None');
But if I set z2 = 3:7 and x2 = 3:7, then it can produce a graph without an error, but I don't know why is this, and how can I achieve what I want to plot?
matlab error-handling colormap
Answers (1)
Walter Roberson
on 23 Sep 2016
x = 0:100:600;
z = 0:100:600;
x2 = 1:3; %the indices of the subset you want to use
z2 = 1:3; %the indices of the subset you want to use
x_chosen = x(x2);
z_chosen = z(z2);
[X, Z] = meshgrid( x_chosen, z_chosen ); %only create the meshgrid from the portion you need
E = X.^2 + Z.^2;
surf(X, Z, E)
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!