繁体   English   中英

MatLab-输入法

[英]MatLab - Inputdlg

我正在尝试使用plot和inputdlg获取图形。但是,尝试越多,我就越困惑:/

另外,我必须找到问题的答案:

  1. 如何在自己的图上显示每个函数?
  2. 您如何正确显示水平轴值?

这就是我试图写的:

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

在此先感谢您的帮助和建议:)

首先,从inputdlg的文档中可以inputdlg ,输出是一个单元格数组,因此您需要对其进行正确索引 例如, if a{1} == 'y'

第二件事,如果我正确地解释了该程序的目的,则需要确定输入函数的形式。 如果您假设用户将提供输入函数字符串作为匿名函数 ,则事情会稍微容易一些。 否则,您需要为func变量创建一个解析器,以正确设置要评估和绘制的函数。 具有符号也使它非常简单,但是我没有它,因此无法构造和测试示例。

话虽这么说,我将假设输入是一个匿名函数:

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

现在我们需要谈谈输入验证。 您的示例似乎已开始研究此问题,但我认为它的运行不像您想要的那样。 按照书面说明,如果输入为空,则会询问您是否确定。 如果是,则尝试打破while循环,如果否,则继续绘制函数。 我会在str2func调用中使用try / catch循环 ,以给用户另一个输入有效函数的机会。 您也可以将用户置于while循环中,直到提供有效的输入功能为止。

尝试一下,再提出更多问题。

暂无
暂无

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

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