Return to "input" after error message

2 Ansichten (letzte 30 Tage)
Jorge Barrio Luna
Jorge Barrio Luna am 17 Aug. 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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 18 Aug. 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

Weitere Antworten (0)

Kategorien

Mehr zu Polar Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by