Return to "input" after error message

조회 수: 2 (최근 30일)
Jorge Barrio Luna
Jorge Barrio Luna 2018년 8월 17일
댓글: Jorge Barrio Luna 2018년 8월 18일

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.

채택된 답변

Image Analyst
Image Analyst 2018년 8월 18일
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
Jorge Barrio Luna
Jorge Barrio Luna 2018년 8월 18일
Thanks for your answer, I solve my problem.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by