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

3 次查看(过去 30 天)
Hey, so I am trying to plot this but I get this error. I'm fairly new to MATLAB so I dont know how to fix this issue. Thank you!
close all;
A1 = csvread('Circle6inch copy');
A2 = csvread('Circle12inchCopy');
A3 = csvread('Circle18inchCopy');
r = [6,12,18];
x = [cos(A1(:,1))*r(1); cos(A2(:,1))*r(2); cos(A3(:,1))*r(3)] ;
y = [sin(A1(:,1))*r(1); sin(A2(:,1))*r(2); sin(A3(:,1))*r(3)] ;
a = [A1(:,2); A2(:,2); A3(:,2)];
z = meshgrid(x,y);
n = length(x);
m = length(y);
for i = 1 : n
for j = 1 : m
z(i,j) = a(j);
end
end
[ii,ii] = unique(z','rows','first');
out = z(:,sort(ii));
z = out;
surf(x,y,a);

回答(1 个)

jean claude
jean claude 2017-12-10
read here you didn't use meshgrid in the right way
  2 个评论
Alexander Quinto
Alexander Quinto 2017-12-10
Is this what I was doing wrong? When I try to plot this it gives me the error saying:
Error using matlab.graphics.chart.primitive.Surface/set Value must be a vector or 2D array of numeric type
Error in matlab.graphics.chart.internal.ctorHelper (line 8) set(obj, pvpairs{:});
Error in matlab.graphics.chart.primitive.Surface
Error in surf (line 150) hh = matlab.graphics.chart.primitive.Surface(allargs{:});
Error in datagraph (line 22) surf(X,Y,Z);
close all;
A1 = csvread('Circle6inch copy');
A2 = csvread('Circle12inchCopy');
A3 = csvread('Circle18inchCopy');
r = [6,12,18];
x = [cos(A1(:,1))*r(1); cos(A2(:,1))*r(2); cos(A3(:,1))*r(3)] ;
y = [sin(A1(:,1))*r(1); sin(A2(:,1))*r(2); sin(A3(:,1))*r(3)] ;
a = [A1(:,2); A2(:,2); A3(:,2)];
n = length(x);
m = length(y);
for i = 1 : n
for j = 1 : m
z(i,j) = a(j);
end
end
[ii,ii] = unique(z','rows','first');
out = z(:,sort(ii));
z = out;
[X,Y,Z] = meshgrid(x,y,z);
surf(x,y,z);
Walter Roberson
Walter Roberson 2017-12-10
With three outputs, meshgrid() is going to output 3D arrays, but the z input to meshgrid needs to be 2D.
Aside from that, your code is dubious. Your lines
for i = 1 : n
for j = 1 : m
z(i,j) = a(j);
end
end
set all rows to have the same content. For example, z(1,5) = a(5), but also z(2,5) = a(5), z(3,5) = a(5) and so on. There does not appear to be much point in that.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by