How to define user-defined code blocks MATLAB?
Mostrar comentarios más antiguos
We have code blocks like 'if' and 'while'. Can we create our own code blocks?
If so, could you explain how to do that? Any answer is appreciated, thank you in advance.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 21 de Jun. de 2016
I am not sure if you are referring to MATLAB itself or to Simulink?
If you are referring to MATLAB then perhaps you want "function" ?? There is no direct MATLAB equivalent to C's {} or what some languages would use begin/end for, partly because in MATLAB if and while and for already require blocks terminated with end, not statements. There is no MATLAB equivalent to C's
if (this) ring_location = mordor;
there is only the MATLAB equivalent to C's
if (this) { ring_location = mordor; }
On the other hand, in C and some other languages, a "block" is a scope that can have local variables that are automatically removed at the end of the block; MATLAB does not have any equivalent to that (nested functions do part of that task, but nested functions cannot be declared inside of control statements in MATLAB.)
You might want to separate out a block of code for style reasons. MATLAB provides code folding and code sections
6 comentarios
Sabri Çetin
el 22 de Jun. de 2016
Editada: Sabri Çetin
el 22 de Jun. de 2016
Adam
el 22 de Jun. de 2016
Define the statements in what context though? The switch statement provides the context for such statements. Without them you just have the statements themselves which can either just sit there where they are or be in functions as Walter says.
A switch block is only there because you want to switch between options, an if statement is only there because you want to run conditional code. Remove them and the rest of the code can remain as it is.
Sabri Çetin
el 22 de Jun. de 2016
Editada: Sabri Çetin
el 22 de Jun. de 2016
Walter Roberson
el 24 de Jun. de 2016
What properties should these proposed code blocks have? Should they have the ability to control the flow, such as if you wanted to define a "do/until" operator? Should they be flexible enough that they could implement a "goto" ? Should they be flexible enough to be able to "goto" out of scope? Should they have scoped variables that automatically disappear when the flow leaves the code block? Should it integrate with the Parallel Computing Toolbox, so that toolbox knows how to analyze such code blocks and automatically determine variable lifetimes and so on? Should the propose code blocks be able to extend parallel computing, such as being able to introduce "threads" that execute in parallel and yet have direct access to graphics?
Or is the question one of how to define something that allows you to mark sections of code that will appear completely in a single shade of blue no matter what the contents of the code? Is it about colorization in the editor rather than about flow control or resource management?
Sabri Çetin
el 2 de Jul. de 2016
Walter Roberson
el 2 de Jul. de 2016
MATLAB does not offer any possibility to implement something like that.
One implementation of that in MATLAB would be
while (p | q)
if (p)
Statement Group 1
else
Statement Group 2
end
end
Categorías
Más información sobre Code Execution en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!