Lin Qiao
Lin Qiao

Reputation: 69

Error: surf plot: Z must be a matrix, not a scalar or vector

I want to have a 2-D color map plot, with following code, but it keeps returning me this error, could anyone help me

x = 0:100:600;

z = 0:100:600;

[X,Z] = meshgrid(x,z);

for z2 = 3:5;

    for x2 = 3:5
        E1 = Z(z2);
        E2 = X(x2);
        E = E1 +E2;
    end
end



figure;

surf(X,Z,E,'EdgeColor','None');

view(2);

xlabel('x','fontsize',20);

ylabel('z','fontsize',20);

colormap jet;

Upvotes: 0

Views: 1658

Answers (1)

Suever
Suever

Reputation: 65460

You need an E entry for each X and Z entry to have a valid surface because you need a "height" for each x/y location.

In your example, E is simply a scalar whereas X and Z contain many values. It seems like you'd want to do something like:

E = Z + X;

surf(X, Z, E, 'EdgeColor', 'none')

If instead you want a plane at a given height (E), then make Z a matrix where all values are equal to E.

surf(X, Z, zeros(size(X)) + E, 'EdgeColor', 'none')

If you only want to plot a subset, you can do something like:

[X, Z] = meshgrid(x(3:5), z(3:5));
surf(X, Z, X + Z, 'EdgeColor', 'None')

Upvotes: 1

Related Questions