CAD/GOTO equivalent

Advertisement


Question
Hi George,

I don't know if you remember me, I just asked you last week about importing headers of an xls file into a m-file script. Now I have another question, I am trying to do something like the function GOTO would do on C++, I read on the internet that that can be done with functions, but since I am writing a script, i cannot create a function in it, at least that is the message I get from the Matlab compiler. Do you know how can I jump back to a previous part of my code in a script if some event happens?

Thanks

Jose

Answer
Hi Jose,

the use of GOTO statements is heavily discouraged since it is a bad way of writing code. That's why its use has been abandoned. You SHOULD use functions instead. While it is true that you cannot create a function in a matlab script, this does not mean that you cannot create your own functions in matalb.
Functions in Matlab are M files that start with the declaration of the function e.g.

function [output1,output2]=function_name(input1,input2,..inputn)
.
.
place your code here
variables with names output1,output2 will be returned when you call this function
.
.
.

The name of this m file should match the name of the function, so in this case the m file would be function_name.m.
Then i can use my function in my scripts by calling it, e.g. function_name(something, something).
I suggest you read matlab's documentation on functions since it is vital that you understand how functions are created.

Best
G.

CAD

All Answers


Answers by Expert:


Ask Experts

Volunteer


George Moustris

Expertise

I have extensive experience in MATLAB which is a CAD for engineers.I can answer many questions on the programming script as well as the use of many toolboxes Matlab uses.However DO NOT ask me about the underlying theory of your projects.I will only answer questions regarding the USE of Matlab.

Experience

I have written many scripts in Matlab utilizing many toolboxes and libraries such as Fuzzy Logic toolbox,Neural Nets,Image processing,Wavelets,Signal processing etc.

Organizations
Aristotle University of Thessaloniki

©2012 About.com, a part of The New York Times Company. All rights reserved.