简体   繁体   English

如何在meshgrid中使用inputdlg

[英]How to use inputdlg in meshgrid

This is an example of the surface mesh plot syntax: I want the variables inside the meshgrid to be done using inputdlg and the equation in the z variable.这是表面网格图语法的例子:我希望中的变量meshgrid用做inputdlg并在方程z变量。

[x,y] = meshgrid(-2:0.1:2);
z = x.*exp(-((x-y.^2).^2 + y.^2)); 
mesh(x,y,z)

One solution that might be interesting is to use anonymous functions.一种可能有趣的解决方案是使用匿名函数。 A function can be parsed from a string using the function str2func() .可以使用函数str2func()从字符串解析函数。 The character arrays within the dialogue boxes can be parsed to integers using str2double() .对话框中的字符数组可以使用str2double()解析为整数。 Depending on how important it is to have the data points for the Z-function and to be able to set the plotting density/interval this may be an option to opt-in for.根据拥有 Z 函数的数据点以及能够设置绘图密度/间隔的重要性,这可能是一个选择加入的选项。

Summary:概括:

• Use anonymous functions → independent variables defined within @() . • 使用匿名函数→ 在@()定义的独立变量。

• Grab input variables from dialogue boxes. • 从对话框中获取输入变量。

• Plot on the specified domain using fmesh() . • 使用fmesh()在指定域上fmesh()

输入对话框和输出绘图结果

%Setting the prompts/field names%
Prompts = {'Axis Minimum','Axis Maximum','Function Equation'};

%Setting the dialog modal title%
Dialog_Title = 'Dialog Title: Change Me';

%Setting the dimensions of each input field%
Field_Dimensions = [1 50];

%Default Inputs/Placeholders%
Default_Inputs = {'-2','2','@(x,y)x.*exp(-((x-y.^2).^2 + y.^2))'};

%Grabbing the answers from the input dialog%
Answers = inputdlg(Prompts,Dialog_Title,Field_Dimensions,Default_Inputs);

%Grabbing the answers from the "Answers" array%
Axis_Minimum = str2double(Answers{1});
Axis_Maximum = str2double(Answers{2});
Z_Function = str2func(Answers{3});

%Plotting the function given the axis boundaries%
fmesh(Z_Function,[Axis_Minimum Axis_Maximum Axis_Minimum Axis_Maximum]);

Using MATLAB version: R2019b使用MATLAB版本:R2019b

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

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