MATLAB中包含多个条件语句的对话框

时间:2013-08-06 09:59:02

标签: matlab if-statement

我正在编写一个程序而且我已经走到了尽头。

程序开始询问:

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

那么,如果用户选择Retrain,我如何重定向if语句来执行Train位?

1 个答案:

答案 0 :(得分:6)

你可以使用这样的东西。

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