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

Chr*_*ndo 0 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') ...
Run Code Online (Sandbox Code Playgroud)

但是我也想要它

    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
Run Code Online (Sandbox Code Playgroud)

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

gra*_*tnz 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.
Run Code Online (Sandbox Code Playgroud)

编辑:正如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
Run Code Online (Sandbox Code Playgroud)