2016-12-20 12 views
-1

数字1〜22を入力します。 (数字が22のため)for-loop時間制限を設定しforループを中断する方法は?

(例)iをタイプするとfigure(1)が表示されます。 )

今私も、私はそれらのすべてを入力し、彼らは22

ともかけて、私は道の終わりを知りたいことができないので、22

にこのループの回数の上限を設定したいですこのループでは、すべての数値(22未満)を入力する必要はありません。

私は謝罪したコードを表示します、私に助言を与えてください。

for TNP=1:23  
**% for-loop's end condition -->1. when i type all 22 number. 
         ->2. when i type 23 to end the for-loop without typing more number.** 

    FN = input('Which figure do you want: ') **% here i would type the number of figure.** 

    if FN==1 
    F1=meshc(z(:,111:268)) 
    grid on 
    hold on 

    elseif FN==2 
    F2=meshc(z(:,269:419)) 
    grid on 
    hold on 

    elseif FN==3 
    F3=meshc(z(:,431:586)) 
    grid on 
    hold on 
. 
. 
. 
    else FN=23 
    close; 
    end 

end 
**% but even i add the 'break' for-loop doesn't end. what is the reason??** 

答えて

0

ここでループを使用することはできません。

ループは、特定の回数だけ特定のコードを繰り返したい場合に使用しますが、ここでは正しく解釈されません。

あなたが望むのは、1から22の入力を受け入れて、対応する数字を表示することです。数字は順不同である必要があります(手動入力は必要ありません)

ループを終了する番号(たとえば-1)を定義してからwhileを使用する必要があります。

FN = input('Which figure do you want (1 to 22, or -1 to exit)?: ') 
while FN ~= -1 
    if FN < 1 | FN > 22 
     disp (['Incorrect option: ' num2str(FN)]); 
    else 
     % your code to handle correct cases here 
    end 
    % now ask again 
    FN = input('Which figure do you want (1 to 22, or -1 to exit)?: ') 
end 
0

私はサムベイノリマキに同意します。あなたの選択したデザインは少し奇妙です。私は彼が書いたものに似た何かのために行くだろう:

function myfcn() 

    % (define Z here) 

    % User input loop   
    while true 

     % Get the number 
     FN = input('Which figure do you want: ');    

     % Don't ever trust the user, even if 
     % the only user is you 
     if ~isnumeric(FN) || ... 
       ~isscalar(FN) || .... 
       ~isfinite(FN) || ... 
       ~isreal(FN) || ... 
       FN > 23 || ... 
       FN < 0 

      warning([mfilename ':invalid_input'],... 
        'Invalid input; please insert a figure number 1-23, or 0 to exit.'); 
      continue; 

     end 

     % It's always good to have an explicit exit condition 
     if FN == 0 
      break; end 

     % Apparently, you want this to be a special case 
     % (Note that this will only close the last figure drawn) 
     if FN == 23 
      close; end 

     % Call a handler to do all the heavy lifting 
     h = myGraphicsHandler(z, round(FN)); 

     % ...Do whatever you want with this handle 

    end 

end 

function h = myGraphicsHandler(z, FN) 

    % These are the only things that seem to be 
    % different between each case 
    rangesTable = {111:268 
        269:419 
        431:586 % ... and so on 
        }; 

    % create the figure 
    h = figure; 
    hold on 
    meshc(z(:, rangesTable{FN})); 
    grid on  

    % ... other operations/figure decorations here 

end 
関連する問題