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

3 views (last 30 days)
Kenneth Lamury on 12 Jul 2016
Commented: Stephen Cobeldick on 12 Jul 2016
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:
...
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
...
IF(REI(1)) 1000,1470,1470
...
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
1 XLIFT(N),PTMOM(N),FUEL(N)
IF(GW(N)) 1565,1150,1150 !1150 continues in the program
...
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
...

#### 1 Comment

Stephen Cobeldick on 12 Jul 2016
@Kenneth Lamury: don't bother converting this to MATLAB code, when you can just call the FORTRAN code from MATLAB:

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

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 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.

### Community Treasure Hunt

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

Start Hunting!

Translated by