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

조회 수: 3 (최근 30일)
Alexander Quinto
Alexander Quinto 2017년 12월 10일
댓글: Walter Roberson 2017년 12월 10일
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