繁体   English   中英

如何在应用程序设计器中实现 surfc?

[英]How do I implement surfc in app designer?

我有一个问题,在 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

但在这里我想将其实现到应用程序设计器中。 我希望用户输入复杂的 function 他们在 x 和 y(非极坐标)的笛卡尔坐标系中,然后向他们显示 plot。 但是当我运行相同的代码并稍作更改时,我总是会收到错误消息:“使用 surfc 时出错。表面 z 必须包含不止一行或一列。”。

这是我的应用程序设计器回调:

% 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 

我希望它会在我设计的应用程序上显示图表,但它只是显示该错误消息。 如何使这个工作?

您正在获取编辑字段的.Value ,因此很可能是一个字符。 然后你使用它就好像你已经评估了一些 function 的xy ,但你没有! 你可能需要像

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

现在您已经从 char 输入,通过 function,实际生成可用于绘图的z

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM