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 Center および File ExchangePolar Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by