Does MATLAB permit the entrance of a loop from some point outside of the loop?

1 view (last 30 days)
I'm an established FORTRAN programmer, and an intermediate MathCad user, who relies on their concepts to understand MATLAB. I've taken MathWorks' "Fundamentals of MATLAB" 24 hr course, and read "MATLAB for Dummies" by Sizemore & Mueller and "Learning MATLAB" by Driscoll. I'm about to start "Basic MATLAB, Simulink, & Stateflow" by Colgren. These references make no mention of a restriction on entering a loop from some point outside of a loop. I cannot see this. (I'm currently using MATLAB version R2016a Student.)
I can visualize a most outer loop containing inner loops. What I cannot understand is how to exit the most inner loop and enter a secondary loop without the 'dreaded' GOTO statement y'all talk about.
FORTRAN:
...
1000 READ(20,106) XXL,SW,CBAR,B2,XMAC,XREF,S1CON,PUNCH,IDUM,IDUN,ITNGO,
1 (ITIT(I),I=1,6) !the most outer loop
IF(SW) 1010,1010,1030 !1010 cycles to end-of-program, 1030 continues with source code
...
91460 READ(20,101) XOCOL,ACOLT,XMODL,XFULD
1460 READ(20,101) (REI(I),I=1,L)
IF(REI(1)) 1000,1470,1470
1470 READ(20,101) (RGJ(I),I=1,L)
...
1565 READ(20,101) ALT,EAS,XMACH,FCW,FCM,FCASP,FCCMSP,XT,SIGMA,THEDCT
IF(EAS) 91460,1460,1570
1570 IF(XMACH -XMHCR) 1580,1600,1600
1580 DO 1590 I=1,L
...
1590 DCML(I)=DCMCLS(I)
GO TO 1680
1600 NOTBL=KK
...
1680 NOTBL=LM
...
1140 CONTINUE
DO 1160 N=1,13
READ(20,101) GW(N),CG(N),XN(N),DESP(N),THR(N),UDE(N),XMAG(N),
1 XLIFT(N),PTMOM(N),FUEL(N)
IF(GW(N)) 1565,1150,1150 !1150 continues in the program
1150 READ(20,101) COND(N),FLAP(N),SF(N),COTNAC(N),SHRCRT(N)
...
As you can see, I have a complicated cycle system that I cannot visualize in MATLAB. It looks like I'm entering an inner loop via an inner loop rather than outer loop containing an inner loop. Is there anyone out there that can solve my dilemma? I'm currently using to read the data with
fgetl(file)
XYc=teaxtscan(file,fmt_num1,1);
XY=XY=cell2mat(XYc);
for i=1:L
REI(i)=XY(i);
end
if REI(1) >= 0 %continue with program, otherwise cycle to reading SW
...

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jul 2016
No, you can only enter loops from their immediate containing code.
If you need to skip portions of the code once, you can set flags. For example instead of
GOTO HERE
while condition_1 %LOOP #1
statement group A
while condition_2 %LOOP #2
LABEL HERE:
statement group B
end %LOOP #2
end %LOOP #1
you can use
skip_group_A = true;
while condition_1
if ~skip_group_A
statement group A
end
while condition_2
statement group B
end
skip_group_A = false;
end
  2 Comments
Kenneth Lamury
Kenneth Lamury on 12 Jul 2016
The second part to your answer looks plausible rather than the first part which uses "GOTO HERE' statement that may not exist in a Student version. Thanks for the assistance.
dpb
dpb on 12 Jul 2016
Edited: dpb on 12 Jul 2016
The first part of Walter's answer was example, not a suggested solution; it illustrates the code structure the second portion illustrates how to implement.
BTW, there's not a "GOTO" in any version of Matlab; it's not just missing in a Student version, it isn't in the Matlab syntax at all (sometimes to my regret; there are places where it can be quite beneficial, Dykstra be ...).
Also, while it's not directly an answer to the question as asked, consider using MEX to convert the Fortran to a mex-file; then you can keep the same code reading the data as it always did; just make an interface to call it/return results from/to Matlab. Might be pretty simple solution in comparison to rewriting complex logic that already functions.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!