Is there command to go line I want?."goto"

254 views (last 30 days)
I usually work with Fortran, C++ and there is command "goto" which goes to line I need. Now in my matlab 12 I could not do it.please help me, thanks for response.
  1 Comment
dpb
dpb on 30 Mar 2014
Besides the previous comment on how to refactor, for Matlab you've got a lot of stuff like the following--
E1=23.*VR*13.605;
E2=4.1*VR*13.605;
E3=1.3*VR*13.605
...
S1=F1*VA*(LOG(S0/E1)-BC2)/E1;
S2=F2*VA*(LOG(S0/E2)-BC2)/E2;
S3=F3*VA*(LOG(S0/E3)-BC2)/E3;
that should be rewritten to use the array notation more like
E=13.605*[23.0 4.1 1.3].'*VR;
...
S=F.*VA.*(LOG(S0./E)-BC2)./E;
etc., ...
Then address the elements of E and S as E(1), S(1), where/if needed.
Also, you're aliasing the builtin Matlab function pi with a less accurate constant (which isn't apparently used, anyway). Don't do this kind of thing in Matlab; it'll bite nastily at some point if you get in the habit.

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 30 Mar 2014
Edited: Azzi Abdelmalek on 30 Mar 2014
There is no such command in Matlab, plus, it's not recommended to use goto. It's hard to read a big code with many goto inside.
You can replace goto by using a better structured code, with command like: while, calling functions, ...
  3 Comments
dpb
dpb on 30 Mar 2014
Edited: dpb on 30 Mar 2014
...In my code it [GOTO] is necessary...
It is always possible to restructure/refactor code to eliminate GOTO. Depending on the actual logic, it may be as simple as IF...ELSE...END or a WHILE...END; in other cases (particularly often error handling) it may need more serious refactoring of the code. try...catch is often useful for such cases.
As for your particular code, two suggestions --
1) Keep the Fortran version but either run the standalone via system or turn it into a mex function, or
2) If it is mandatory to convert to Matlab native code then at least provide a clean snipped version that contains only the relevant sections you're having difficulty with in the native Fortran including line numbers. It's impossible in what you've give to determine where the branches are going and there are large chunks that don't appear to have any bearing on the question.
ADDENDUM:
OK, as an aid to get you started, I snipped the code down to just the branches and a line or two between so it would fit in a page to see the overall structure. Then I did a couple of the transformations as starters...
for i=1:10;
...
VRLL=0.0;
% 10 continue
flag10=true;
while flag10
DD=LOG(RR)/(-SS);
VRLL=VRLL+DD;
%if VRLL>=RT; GO TO 50, end
if VRLL>=RT, continue, end % skip to next iteration
RN=rand(1);
%if RN<=RG1; GO TO 20, end
if RN>RG1 % execute next instead skipping on negated logic
RN=rand(1);
if RN>=RN2; GO TO 13, end
if RN>=RN1; GO TO 14, end
EE=E1;
GO TO 15
13 continue
GO TO 15
14 continue
15 continue
EE1=EE;
16 continue
EE=EE1/(1.0-RN+RN*EE1/WMX);
RN=rand(1);
if RN>=(1.-BC2*EE/WMX); GO TO 16, end
GO TO 30
end % 20 target end ←
if RN>=R2; GO TO 25, end
if RN>=R1; GO TO 26, end
EE=E1;
GO TO 30
25 continue
GO TO 30
26 continue
30 continue
de=de+EE*1.E-6;
re12=re12-EE*1.E-6;
if re12-rm<=0.05; GO TO 50, end
tp=tp+DD/b12;
end % GO TO 10 conditional loop
50 continue
...
end
1) the GOTO 10 is just an indefinite loop -- I replaced it with a WHILE...END clause where I defined a flag variable that may or may not in the end be of any need; I didn't go far enough to decipher just how the loop does finally get terminated so where setting the flag variable false is the right spot, specifically.
2) Then, there's no GOTO 11 so that label can be eliminated which leads to...
3) the GOTO 50 is just causing a skip of the body of the for...end loop which is, in Matlab continue so the line
if VRLL>=RT, GOTO 50, end
becomes
if VRLL>=RT, continue, end
There's another of these later on; when that one's taken care of the same way then the label 50 can be dispensed with.
4) The branch
if RN<=RG1; GO TO 20, end
causes conditional skipping of the code to label 20 which is the same thing as the conditional execution of the code as
if RN~<=RG1; {CODE BLOCK}, end
I just changed the conditional from <= to > and added the end at the target label and indented for legibility...
if RN>RG1 % execute next instead skipping on negated logic
The remainder is just a continuation of the exercise as above.
John D'Errico
John D'Errico on 30 Mar 2014
I totally agree with dpb. You can always formulate your code NOT to use goto.
If you think you need it, sorry, but you are just avoiding the task of learning the tools present in MATLAB.

Sign in to comment.

More Answers (2)

Vishwanath Salokye
Vishwanath Salokye on 30 Mar 2014
Hi,
Their is no such command in MATLAB. using function call you can jump to the particular location. But in matlab their are two types of workspace one is base work space and other one is function workspace. When you enter in to any function base workspace will be empty you have to use "evalin" command to get base workspace value to function workspace
  2 Comments
dpb
dpb on 30 Mar 2014
For this purpose don't even think about it... :)

Sign in to comment.


Guruprasad S
Guruprasad S on 11 Apr 2022
Hi, I wrote a function to read the input whole number between 2 and 6. The conditions are, if the input number is <=2 or >=6, it must repeat the function with an error message. Similarly, if the number entered is a fraction, it must repeat the function with an error message. This might be an answer to goto function.
function new_file
x = input('input a whole number between 2 and 6 ');
if x==3
fprintf('three');
elseif x==4
fprintf('four');
elseif x==5
fprintf('five');
elseif x<=2 || x>=6;
fprintf('read the input properly\n');
new_file
else
fprintf('input whole number only\n')
new_file
end
  2 Comments
Steven Lord
Steven Lord on 11 Apr 2022
There is no goto function in MATLAB.
For this application rather than having your function call itself recursively just use a while loop. while the user has not entered a valid input, ask them for a valid input.
dpb
dpb on 12 Apr 2022
Besides @Steven Lord's suggestion on refactoring/restructuring, note that the above function never returns the input value -- while perhaps not the best use of recursion, with that correction looks like it should work just at a glance...excepting you'll then also return all the wrong answers besides the last one unless you do something about that.
Also, use disp() instead of fprintf for your debugging of the results -- your lines have no \n so will run on to the command line prompt.
While I still would no recommend it, the following solves the problems --
function x=new_file
x = input('input a whole number between 2 and 6 ');
if x==3
disp('three');
elseif x==4
dips('four');
elseif x==5
disp('five');
elseif x<=2 || x>=6;
fprintf('read the input properly\n');
clear x
new_file
else
fprintf('input whole number only\n')
clear x
new_file
end
end

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!