Matlab GUI 中的用户输入

发布于 2024-11-09 22:17:26 字数 1485 浏览 3 评论 0原文

嘿大家, 我正在构建一个图形用户界面,其中有一个编辑框,等待用户输入名称。

目前,我使用此代码强制用户提供合法名称:

NewPNUName = get(handles.nameOfNewPNU, 'String');
if ( isempty(NewPNUName) ||...
        strcmp(NewPNUName,'Enter the name for the new PNU') )
    errordlg('Please enter a name for the new PNU.');
elseif (~ischar(NewPNUName(1)))
    errordlg('The PNU name should start with a letter.');
else
    handles.NewPNUName = NewPNUName;
end

if (~isempty(handles.NewPNUName))
% Do all the things needed if there is a legit name
end

如果用户没有编写合法名称,那么它所做的就是什么。 我想要它做的是制作一个带有编辑框的弹出窗口,要求用户再次输入想要的名称,直到它是合法名称。

感谢您的帮助!

编辑: 根据 @woodchips 的建议,我将代码更正为以下内容:

NewPNUName = get(handles.nameOfNewPNU, 'String');
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
    ~strcmp(NewPNUName,'Enter the name for the new PNU');
while (~ValidName)

    if ( isempty(NewPNUName) ||...
            strcmp(NewPNUName,'Enter the name for the new PNU') )
        NewPNUName = char(inputdlg('Please enter a name for the new PNU.','No name entered'));
    elseif (~isletter(NewPNUName(1)))
        NewPNUName = char(inputdlg('The name of the new PNU should start with a letter. Please enter a new name',...
            'Invalid name entered'));
    else
        allConds = 'are met'
    end

    ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
        ~strcmp(NewPNUName,'Enter the name for the new PNU');
end

Hey all,
I am building a gui in wich there is an edit box, waiting for the user to write a name.

Currently I force the user to give a legitimate name with this code :

NewPNUName = get(handles.nameOfNewPNU, 'String');
if ( isempty(NewPNUName) ||...
        strcmp(NewPNUName,'Enter the name for the new PNU') )
    errordlg('Please enter a name for the new PNU.');
elseif (~ischar(NewPNUName(1)))
    errordlg('The PNU name should start with a letter.');
else
    handles.NewPNUName = NewPNUName;
end

if (~isempty(handles.NewPNUName))
% Do all the things needed if there is a legit name
end

What it does is nothing if the user didn't write a legit name.
What I want it to do is to make a popup with an edit box, asking the user to input the wanted name again, until it is a legitimate name.

Thanks for the help!

EDIT:
following @woodchips advice I corrected my code to the foloowing:

NewPNUName = get(handles.nameOfNewPNU, 'String');
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
    ~strcmp(NewPNUName,'Enter the name for the new PNU');
while (~ValidName)

    if ( isempty(NewPNUName) ||...
            strcmp(NewPNUName,'Enter the name for the new PNU') )
        NewPNUName = char(inputdlg('Please enter a name for the new PNU.','No name entered'));
    elseif (~isletter(NewPNUName(1)))
        NewPNUName = char(inputdlg('The name of the new PNU should start with a letter. Please enter a new name',...
            'Invalid name entered'));
    else
        allConds = 'are met'
    end

    ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
        ~strcmp(NewPNUName,'Enter the name for the new PNU');
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-11-16 22:17:26

因此,在一段代码周围放置一个 while 循环,生成一个 inputdlg 框。将 while 循环的条件设置为结果有效。

So, put a while loop around a block of code, that generates an inputdlg box. Set the condition on the while loop to be that the result is a valid one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文