简体   繁体   中英

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.

[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() . The character arrays within the dialogue boxes can be parsed to integers using 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.

Summary:

• Use anonymous functions → independent variables defined within @() .

• Grab input variables from dialogue boxes.

• Plot on the specified domain using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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