A multi conditional goto-like implementation in Matlab

A suggestion on how to implement in Matlab, a FORTRAN subroutine with a set of complicated "go tos"
Updated 10 Jun 2010

View License

1. Introduction

Enough debate has been devoted from time to time to the luck of "go to" in Matlab.
Ourselves being somewhat devoted followers of structured programming, we rarely use
it in our FORTRAN applications and implementations. However, one cannot help that
sometimes there exists this need of a "quick and dirty" redirection within a code.
Nevertheless, we are fully aware that a "go to" implementation in Matlab would lead
to a lot of unacceptable coding. The "for entertaining purposes" "go to" implementation
of Husam Aldahiyat as presented in File Exchange of Mathworks triggered us into trying
it in a long waiting FORTRAN to Matlab conversion of a subroutine, that needs to perform
a set of conditional "go tos". This "go to" did not actually work for us. Its main
problem is that it is designed to handle "go to"s that do not transfer control to other
.m code for the same application. Consequently, we decided that it was high time to try
and implement our FORTRAN subroutine with conditional "go tos" in Matlab, using structured

2. Our conditional "go to" problem

We need to guide our code, so that to proceed to a series of different steps, depending
on choices. As it happens our choices are quite many. In order to differentiate these
choices from each other we gave each one a unique number between 0 and 63. It is obvious
that each choice number can be a result of summing up 0, 1, 2, 4, 8, 16 and 32 or
equivalently 0, 2^0, 2^1, 2^2, 2^3, 2^4, and 2^5. In our code if sum = 0 nothing happens,
if sum = 1 then task#1 is performed, if sum = 2 then task#2 is performed, if sum = 4 then
task#4 is perforned, if sum = 8 then task#8 is performed, if sum = 16 then task#16 is
performed and finally if sum = 32 no specific task is assigned and this choice number
is reserved for provisional use. In other words our code would not exploit all sum values
available. The actual implementation is useful for the calculation of chosen thermophysical
properties out of a set of available equations for water. To our best knowledge the
original of this implementation is to be found in:

Hendricks R.C., Peller R.C., Baron A.K., "WASP - A Flexible FORTRAN IV Computer Code of
Calculating Water and Steam Properties", Report No. NASA Technical Note TN D-7391, NASA,
Washington D.C. 20546, USA, 1973.

3. The FORTRAN implementation

It can be found in the .zip file as total.f subroutine

4. The Matlab implementation

It can be found in the .zip file as total.m function

5. Some discussion

It was not that straightforward but it is evident that a FORTRAN code with such a
complicated set of conditional "go tos" invoking some programming decisions, could be
implemented in Matlab as well. In this particular case the JPCi variables which govern
the conditional choice decisions are included in the m code function, while they are
communicated to the FORTRAN subroutine, using COMMON blocks calling a BLOCK DATA subprogram.
Furthermore, attention should be paid that the implementation presented is based in a system
of "functions that call other functions". Such a programming environment in Matlab calls for
using only static variables that should be communicated "to and from" as inputs and outputs
of functions and not as parts of global scripts.

S.T. Pachitsas, Student of Mechanical Engineering
N.P. Petropoulos, Lecturer
Nuclear Engineering Department
School of Mechanical Engineering
National Technical University of Athens
15780 Athens, GREECE

Cite As

Petropoulos (2024). A multi conditional goto-like implementation in Matlab (https://www.mathworks.com/matlabcentral/fileexchange/27591-a-multi-conditional-goto-like-implementation-in-matlab), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Fortran with MATLAB in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes


As from June 10, 2010, the total.m file has been replaced with a newer version, which handles returns from included functions more efficiently. Unfortunately the original version was a bit buggy and it contained some unnecessary loops.