Is there command to go line I want?."goto"
Show older comments
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
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.
Accepted Answer
More Answers (2)
Vishwanath Salokye
on 30 Mar 2014
1 vote
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
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
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
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
Categories
Find more on Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!