How to skip command lines inside the loop

1 回表示 (過去 30 日間)
Muharrem Askin
Muharrem Askin 2012 年 6 月 29 日
I have a big while loop in some part of my project. Inside this loop, there are many operations going on and the results are checked for the convergency in the end. If the result is convergent, the loop ends but if it is non-convergent, I need to change the parameter and go back to the beginning of the loop again.
Here comes my question : I have 5 parameters to modify and all of the parameters should be checked in order. For example first parameter is STALLH Y and I would like to make it STALLH N if the result is non convergent and go to the beginning of the loop. When I evaluate the result again, I would like to modify the 2nd parameter IDLEPITCH 90 to IDLEPITCH 91. This will be like that until the end of the 5th parameter. But everytime I come to the parameter modification part, the code will check the previous parameters again which I dont want. Is there any suggestion for skipping some lines in command order according to the conditional cases?

回答 (1 件)

Adam Filion
Adam Filion 2012 年 6 月 29 日
You can use either an if/else or switch/case structure to conditionally execute sections of code. The examples section in their documentation pages below is a good place to get started with them.
  1 件のコメント
Muharrem Askin
Muharrem Askin 2012 年 6 月 29 日
I know how to use switch/case structure but it does not solve my problem. Because When I determine the non-convergent case, I need to read the entire data file line by line and write to another file. Among those many lines, there are also lines for my 5 parameters and I would like to modify them in an order. But the 5 parameter lines are not in the same order inside the data file. For exp, I would like to check and modify the parameters in this order : STALLH Y IDLEPITCH 90 WDIR 24 DFLAP 0.007 DEDGE 0.008
but these parameter lines are not in this order inside the file and the order is like that .(of course each one does not come after another .There are many lines between each other) STALLH Y DFLAP 0.007 DEDGE 0.008 WDIR 24 IDLEPITCH 90
So If I use switch/case structure, the program will take the first true case and exit from the switch structure. In order to show what I am talking about, I pasted the code below.Do you know how can I solve this problem ?
infile = fopen( 'C:\Documents and Settings\guestaskin\Desktop\model\idling.$PJ', 'rt' ); outfile = fopen( 'C:\Documents and Settings\guestaskin\Desktop\model\idling1.$PJ', 'wt' );
while ~feof(infile) line= fgetl(infile); toks = textscan(line,'%s%s',1); if ~isempty(toks)&& iscell(toks{1}) switch toks{1}{1} case{'STALLH'} line=sprintf('%s %s',toks{1}{1},textdata{k}{2}{1}); case{'IDLEPITCH'} line=sprintf('%s %s',toks{1}{1},textdata{k}{2}{2}); case{'WDIR'} line=sprintf('%s %s',toks{1}{1},textdata{k}{2}{3}); case{'DFLAP'} line=sprintf('%s %s',toks{1}{1},textdata{k}{2}{4}); case{'DEDGE'} line=sprintf('%s %s',toks{1}{1},textdata{k}{2}{5}); end end fprintf(outfile,'%s\n%s\n',line ); end fclose(infile); fclose(outfile);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by