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

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 日

2 投票

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 件のコメント

amine&&
amine&& 2016 年 7 月 6 日
Hello Stephen. You have not understood my question. My problem is in the values that will make the variable Z.
Stephen23
Stephen23 2016 年 7 月 6 日
編集済み: Stephen23 2016 年 7 月 6 日
Your question shows an error due to the size of the input to surf. You implied that you wanted this error fixed, so I fixed this error for you. This error has nothing to do with the Z-limits of the plot.
Calculating Z-values and setting the Z-limits when plotting are two totally different things. Your code seems to get these two things a bit mixed up.
To set the plot Z-limits you simply need to use zlim, and the code fix that I showed in my answer:
>> [X,Y] = meshgrid(0:.2:1, 0:.2:1);
>> Z = 10*X.*Y;
>> surf(X,Y,Z)
>> zlim([-5,5])
amine&&
amine&& 2016 年 7 月 6 日
Thanks so much Stephen.
amine&&
amine&& 2016 年 7 月 6 日
Hello Stephen, i did not understand the usefulness of the following :
>> Z = 10*X.*Y;
Thanks!
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 日

1 投票

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);

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

質問済み:

2016 年 7 月 6 日

回答済み:

2020 年 12 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by