【问题标题】:How to repeat an input if the user didn't type in anything and edit the entered input in matlab?如果用户没有输入任何内容并在matlab中编辑输入的输入,如何重复输入?
【发布时间】:2012-12-23 10:04:54
【问题描述】:

例如我有


x = input('value = ?')

如果用户只是在没有任何输入的情况下按回车,我该如何重复此命令,并且我想知道无论如何我可以编辑我的输入,就像在我键入后按“退格键”一样。

就像我现在有两个输入变量'


x = input('??');
y = input('???');
如果在提示输入函数 y 时插入了 x 的第一个输入数据,我可以编辑我的第一个输入吗?

衷心感谢任何愿意提供帮助的人。

第一种情况:

我想要一个类似这样的代码


x = input('value = ?');
while x == %%no input%%
    x = input('value = ?'); %prompt the input command again
end


while x==error %% I want x in numeric input only
    x = input('value = ?'); %prompt the input command again
end

【问题讨论】:

标签: matlab


【解决方案1】:

对于第一种情况:

x = input('??'); % if the user just hits 'enter' x is an empty variable
while isempty( x )
   x = input('??');
end

为了更健壮的方法

x = str2double( input('Your input here:', 's') );
while ~isnan( x )
    x = str2double( input('Your input here:', 's') );
end

input('??', 's') 命令“按原样”返回输入,并且不会尝试将其转换为数字。转换是通过命令str2double 完成的。现在,如果输入不是数字(双精度),则str2double 返回NaN。这可以通过isnan 捕获。

希望这对你有用。

【讨论】:

    【解决方案2】:

    对空白重复,

    x=''
    while isempty(x)
      x=input('value=');
    end
    

    对于非数字,你可以使用类似的东西

    x=''
    while isempty(x)
      try
         x=input('value=')
      catch me
         fprintf('enter a number\n')
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 2021-12-17
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多