0

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

程序开始询问:

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 语句以执行训练位?

4

1 回答 1

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
于 2013-08-06T10:13:00.973 回答