Return to "input" after error message

2 vues (au cours des 30 derniers jours)
Jorge Barrio Luna
Jorge Barrio Luna le 17 Août 2018
Commenté : Jorge Barrio Luna le 18 Août 2018

Hello everyone, I'm new using Matlab. I am generating a function in which the user has to enter some input values ​​with "input (' ')", and if these values ​​are not what is desired, it returns an error. What I want to do is that if it returns an error, instead of closing the function and starting again from the beginning, go back to the sentence "input (' ')". This is part of my code:

    disp('INTRODUZCA EL VALOR DE LA DECLINACIÓN')
    Dg=input('Grados de la declinación del astro (con signo - si es negativo): ');
    if Dg==-90 || Dg==90
        D=Dg;
    elseif Dg<-90 || Dg>90
        error('Valores mal introducidos. El valor de los grados debe estar entre -90 y 90')
    else
        Dm=input('Minutos de la declinación del astro: ');
        if Dm<0 || Dm>59
            error('Valores mal introducidos. El valor de los minutos debe estar entre 0 y 59')
        end
        Ds=input('Segundos de la declinación del astro: ');
        if Ds<0 || Ds>59
            error('Valores mal introducidos. El valor de los segundos debe estar entre 0 y 59')
        end
        if Dg<0
            D=-(abs(Dg)+Dm/60+Ds/3600);
        elseif Dg>=0
            D=Dg+Dm/60+Ds/3600;
        end
    end

For example, if I make a mistake when entering the seconds (Segundos), re-enter them instead of closing the function, as I show below

Grados de la declinación del astro: -15
Minutos de la declinación del astro: 35
Segundos de la declinación del astro: 100
Error using FunctionName (line **)
Valores mal introducidos. El valor de los segundos debe estar entre 0 y 59
Segundos de la declinación del astro: 

Thanks.

Réponse acceptée

Image Analyst
Image Analyst le 18 Août 2018
Put it in a while
tryAgain = true
while tryAgain
tryAgain = false % Assume it will be okay
dg = input(...
if some condition is bad, like variable out of range.
tryAgain = true
end
if some other condition is bad, like variable out of range.
tryAgain = true
end
end
  1 commentaire
Jorge Barrio Luna
Jorge Barrio Luna le 18 Août 2018
Thanks for your answer, I solve my problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Polar Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by