필터 지우기
필터 지우기

Error : Value must be a vector or 2D array of numeric type

조회 수: 47 (최근 30일)
amine&&
amine&& 2016년 7월 6일
답변: Hamid Reza N.D 2020년 12월 5일
I want to draw my function of two variable so that the graph gives me Z values between -5 and 5. I used this code
[X,Y,Z] = meshgrid(0:.2:1, 0:.2:1, -5:.2:5);
Z = arrayfun(@mFunction, X, Y);
surf(X,Y,Z)
but i get the following error :
Error using surf (line 57)
While setting the 'XData' property of Surface:
Value must be a vector or 2D array of numeric type
Thanks!
  댓글 수: 1
KSSV
KSSV 2016년 7월 6일
what is that mFunction? It seems the output of mFunction is not double. Post mFunction here.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일
The problem is how you are calling meshgrid, because calling it with three input arguments causes the outputs to be 3D arrays, not 2D arrays. So the solution is to simply call it like this:
[X,Y] = meshgrid(0:.2:1, 0:.2:1);
and the rest of your code will work fine.
Debugging for Beginners
This bug is easy to find: The error message tells you that the inputs to surf must be 2D arrays, but that they are not. So lets start by having a look at those arrays:
>> [X,Y,Z] = meshgrid(0:.2:1, 0:.2:1, -5:.2:5);
>> size(X)
ans =
6 6 51
Does 6x6x51 look like a 2D array to you? No, clearly the 51 on the third dimensions makes it a 3D array. Why is it a 3D array? Lets actually read the documentation for meshgrid: it clearly states that for three inputs " [X,Y,Z] = meshgrid(xgv,ygv,zgv) produces three-dimensional coordinate arrays." Now we know why the arrays are 3D: because you called meshgrid with three inputs. The documentation also tells us how to make 2D arrays, but I am sure you can figure this out yourself.
  댓글 수: 6
Stephen23
Stephen23 2016년 7월 6일
I do not have your mFunction, so I had to invent some data to test my code on. That is what that line does. If I did not invent some fake data, how would you expect me to test my code ?
amine&&
amine&& 2016년 7월 6일
편집: amine&& 2016년 7월 6일
Ok i understand. Thank you Stephen.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Hamid Reza N.D
Hamid Reza N.D 2020년 12월 5일
Hi. i wana draw my function of three variable. this is my code and i got those Errors. what shloud i do?
>> Q=[1 2 3;4 5 6;7 8 9];
>> S=[0;1;2];
>> u1=linspace(-10,10,20);
>> u2=linspace(-10,10,20);
>> u3=linspace(-10,10,20);
>> [U1,U2,U3]=meshgrid(u1,u2,u3);
>> L=1/2*(Q(1,1)*U1.^2+(Q(1,2)+Q(1,3)+Q(2,1)+Q(2,3)+Q(3,1)+Q(3,2))*U1.*U2.*U3+Q(2,2)*U2.^2+Q(3,3)*U3.^2)+S(1)*U1+S(2)*U2+S(3)*U3;
>> meshc(U1,U2,U3,L)
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 mesh (line 143)
hh = matlab.graphics.chart.primitive.Surface('XData',x,'YData',y,'ZData',z,'CData',c,...
Error in meshc (line 58)
hm = mesh(cax, x, y, z, c);

카테고리

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