【问题标题】:A dialog with multiple conditional statements in MATLABMATLAB 中包含多个条件语句的对话框
【发布时间】:2015-03-08 12:23:10
【问题描述】:

我正在编写一个程序,但我走到了死胡同。

程序开始询问:

button = questdlg('Would you like to train or test the network?', ...
'Artificial Neural Network', 'Train', 'Test', 'Exit', 'Exit');
if strcmp(button,'Train') ... 

elseif strcmp(button,'Test') ...

elseif strcmp(button,'Exit') ...

但我也想让它问

    button = questdlg('Would you like to train or test the network?', ...
    'Artificial Neural Network', 'Train', 'Test', 'Exit', 'Exit');

    if strcmp(button,'Train') ... %do that thing 

    %but if the user wants to retrain in again I want to ask again
    A = questdlg('Would you like to retrain or test the network?', ...
        'Artificial Neural Network', 'Retrain', 'Test', 'Exit', 'Exit');

    if strcmp (A, 'Retrain') do the first step as it is chosen the Train bit

    elseif strcmp(button,'Test') ...

    elseif strcmp(button,'Exit') ...

end

如果用户选择重新训练,我如何重定向我的 if 语句以执行训练位?

【问题讨论】:

  • 你知道while循环吗?
  • 是的..但我从未使用过它
  • 如果用户选择训练,那么它将使用 ANN 训练数据,然后保存数据。我只想问用户他是否想重新训练或进入下一步。如果他想重新训练它,那么程序将与火车相同,但结果不同

标签: matlab if-statement


【解决方案1】:

你可以使用这样的东西。

button = questdlg('Would you like to train or test the network?', ...
'Artificial Neural Network', 'Train', 'Test', 'Exit', 'Exit');

% Loop until the user selects exit 
while ~strcmp(button,'Exit')

    % Button can be one of Exit, Test, Train or Retrain.
    % We know it's not Exit at this stage because we stop looping when Exit is selected.

    if strcmp(button,'Test')
        disp('Test');
    else
        % Must be either Train or Retrain
        disp('Training');
    end

    % We've done testing or training.  Ask the user if they want to repeat
    button = questdlg('Would you like to retrain or test the network?', ...
        'Artificial Neural Network', 'Retrain', 'Test', 'Exit', 'Exit');\

end  % End of while statement.  Execution will unconditionally jump back to while.

编辑:正如 Lucius 指出的那样,您也可以使用 switch 语句来做到这一点,这会使选择更加清晰。

button = questdlg('Would you like to train or test the network?', ...
'Artificial Neural Network', 'Train', 'Test', 'Exit', 'Exit');

while ~strcmp(button,'Exit')

    switch button
        case 'Test'
            disp('Test');
        case {'Train','Retrain'}
            disp('Training');
    end

    button = questdlg('Would you like to retrain or test the network?', ...
        'Artificial Neural Network', 'Retrain', 'Test', 'Exit', 'Exit');
end

【讨论】:

  • 我真的没有理解你在那里所做的事情。你能解释一下吗?
  • :) 太慢了 ;)... 只需添加一件事:我会使用 switch,但在这种情况下 if 可以完成工作...
  • 是的,disp('Test') 只是代码的占位符。您可能希望将测试和训练代码放入单独的函数 (mathworks.com.au/help/matlab/ref/function.html) 中,然后调用适当的函数而不是 disp('...')。
  • 如果用户选择退出,程序将在结束语句之后继续执行。在我的示例代码中,结束语句之后没有任何内容,因此程序执行将停止,但您可以添加更多代码,您希望在退出之前发生一些事情。
  • 我发布的代码总是会询问用户想要做什么,直到他们选择退出。我不知道你的训练代码是做什么的。如果训练本身就是某种循环,那么您需要有一种退出该循环的方法。我想你的原件现在已经得到了回答。也许您需要提出一个与您的实际训练代码相关的新问题。
猜你喜欢
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多