[英]How do I implement surfc in app designer?
I have a problem, in matlab i can code to graph 3D plot of complex function with surfc this is my code for that:\我有一个问题,在 matlab 中,我可以使用 surfc 编码为复杂的 function 的图形 3D plot 这是我的代码:\
figure
X=-2*pi:0.2:2*pi;
Y=-2*pi:0.2:2*pi;
[x,y]=meshgrid(X,Y);
z=3*x+5i*y;
sc=surfc(x,y,abs(z))
xlabel('Re(x)');
ylabel('Im(y)');
colormap jet
colorbar
But here i want to implement this into app designer.但在这里我想将其实现到应用程序设计器中。 I want user to input the complex function they have in cartesian with x and y (not polar), and then showing that plot to them.我希望用户输入复杂的 function 他们在 x 和 y(非极坐标)的笛卡尔坐标系中,然后向他们显示 plot。 But when I run with the same code with a little bit of changes, I always get the error message: "Error using surfc. The surface z must contain more than one row or column.".但是当我运行相同的代码并稍作更改时,我总是会收到错误消息:“使用 surfc 时出错。表面 z 必须包含不止一行或一列。”。
Here is my app designer callbacks:这是我的应用程序设计器回调:
% Button pushed function: MakeGraphButton
function MakeGraphButtonPushed(app, event)
x=-2*pi:0.2:2*pi;
y=-2*pi:0.2:2*pi;
[x,y]=meshgrid(x,y);
z=app.ComplexFunctionEditField.Value;
axes(app.UIAxes);
surfc(x,y,abs(z))
axis equal
grid on
end
end
I expect that it will show the graph on the app that i design, but it just showing that error message.我希望它会在我设计的应用程序上显示图表,但它只是显示该错误消息。 How to make this works?如何使这个工作?
You're taking the .Value
of an edit field, so most likely that's a char.您正在获取编辑字段的.Value
,因此很可能是一个字符。 You're then using it as if you've evaluated some function of x
and y
, but you haven't!然后你使用它就好像你已经评估了一些 function 的x
和y
,但你没有! You might need something like你可能需要像
% Setup...
x = -2*pi:0.2:2*pi;
y = -2*pi:0.2:2*pi;
[x,y] = meshgrid(x,y);
% Get char of the function from input e.g. user entered '3*x+5i*y'
str_z = app.ComplexFunctionEditField.Value;
% Convert this to a function handle. Careful here, you're converting
% arbitrary input into a function!
% Should have more sanity checking that this isn't something "dangerous"
func_z = str2func( ['@(x,y)', str_z] );
% Use the function to evaluate z
z = func_z( x, y );
Now you've gone from a char input, via a function, to actually generating values of z
which can be used for plotting现在您已经从 char 输入,通过 function,实际生成可用于绘图的z
值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.