简体   繁体   English

MatLab-输入法

[英]MatLab - Inputdlg

I'm trying to get graphics with plot and inputdlg.However, the more I tried, the more I get confused :/ 我正在尝试使用plot和inputdlg获取图形。但是,尝试越多,我就越困惑:/

Also, I have to find answers of question: 另外,我必须找到问题的答案:

  1. How would display each function on its own plot? 如何在自己的图上显示每个函数?
  2. How would you display horizontal axis values correctly? 您如何正确显示水平轴值?

And this is what I'd tried to write: 这就是我试图写的:

function Function2()

   while 1

      prompt={'Fonksiyonunuzu Giriniz:'};
      name='Grafik Çizici';

      func=inputdlg(prompt,name);

      if  isempty(func)==1

         prompt={'Are you sure, press y or n:'};
         a=inputdlg(prompt);

         if 'y' ;
            break;
         end

      else 
         plot(func)

      end
   end
end

Thanks in advance for helping and suggestions :) 在此先感谢您的帮助和建议:)

First thing, from the documentation for inputdlg , the output is a cell array, so you need to index it properly . 首先,从inputdlg的文档中可以inputdlg ,输出是一个单元格数组,因此您需要对其进行正确索引 eg if a{1} == 'y' 例如, if a{1} == 'y'

Second thing, if I'm interpreting the purpose of this program correctly you're going to need to decide what form you want your input functions to be. 第二件事,如果我正确地解释了该程序的目的,则需要确定输入函数的形式。 If you assume that the user will provide the input function string as an anonymous function , things are slightly easier. 如果您假设用户将提供输入函数字符串作为匿名函数 ,则事情会稍微容易一些。 Otherwise you need to create a parser for your func variable to properly set up the function to evaluate and plot. 否则,您需要为func变量创建一个解析器,以正确设置要评估和绘制的函数。 Having the symbolic also makes it pretty simple but I don't have it so I can't construct and test examples. 具有符号也使它非常简单,但是我没有它,因此无法构造和测试示例。

That being said I'm going to assume the input is an anonymous function: 话虽这么说,我将假设输入是一个匿名函数:

function plotanonymous()

% Prompt for function to plot
funcprompt = 'Input Anonymous Function to Plot:';
funcprompttitle = 'This is a popup';
func_str = inputdlg(funcprompt, funcprompttitle); % My input will be @(x) x.^2

% Add input validation here
func_anon = str2func(func_str{1}); % Convert function from string to anonymous function

% Prompt for evaluation limits
limitprompt = {'Input Lower Limit'; 'Input Upper Limit'; 'Input Spacing Interval'};
limitprompttitle = 'This is a popup';
evallimits_str = inputdlg(limitprompt, limitprompttitle);

% Add input validation here
% Set up data to plot
evallimits_dbl = str2double(evallimits_str); % Convert to double
x = evallimits_dbl(1):evallimits_dbl(3):evallimits_dbl(2); % Set up x data
y = func_anon(x); % Evaluate input function for input x


% Plot data
% Create a handles structure so we can easily modify properties
h.myfig = figure;
h.myplot = plot(x,y);
end

Now we need to talk about input validation. 现在我们需要谈谈输入验证。 Your example seems to have started to look at this but I don't think it's functioning like you'd want. 您的示例似乎已开始研究此问题,但我认为它的运行不像您想要的那样。 As written, if the input is empty it asks if you're sure. 按照书面说明,如果输入为空,则会询问您是否确定。 If yes, you attempt to break out of the while loop, if no you go ahead and plot the function. 如果是,则尝试打破while循环,如果否,则继续绘制函数。 I would use a try/catch loop with the str2func call to give the user another chance to input a valid function. 我会在str2func调用中使用try / catch循环 ,以给用户另一个输入有效函数的机会。 You could also place the user in a while loop until a valid input function is provided. 您也可以将用户置于while循环中,直到提供有效的输入功能为止。

Give this a go and come back with more questions. 尝试一下,再提出更多问题。

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

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