How to define user-defined code blocks MATLAB?

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.

 採用された回答

Steven Lord
Steven Lord 2016 年 6 月 22 日

0 投票

If you're trying to overload the end keyword (in the context of an indexing expression for an instance of your user-defined class) follow the instructions on that documentation page for how to do so.
----------------
For all other keywords (as listed in the output of the iskeyword function; some examples are if, for, switch, classdef, etc.) or for overloading end in the context of ending a code block started by another keyword, the first part of the first step to do so is to go here.
Once you have completed the first step, the second step is to discuss your proposed new keyword with your manager and other interested parties. They will walk you through the rest of the steps.
----------------
A less strenuous approach, but one that requires additional time and is not guaranteed to succeed, is to contact Technical Support and ask them to file an enhancement request for your new keyword. Describe what you want that new keyword to do and how you would use it if it existed. The developers will review that enhancement request and may choose to implement that new keyword or a variant of it.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 6 月 21 日

1 投票

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 件のコメント

Sabri Çetin
Sabri Çetin 2016 年 6 月 22 日
編集済み: Sabri Çetin 2016 年 6 月 22 日
I unfortunately do not know about C or any other programming language. I wanted exactly code blocks.
We have if and switch code blocks, with them we can control the flow. Now, imagine that switch statements did not exist and one user wants to define such statements for some reason. Can this user define such statements ?
Adam
Adam 2016 年 6 月 22 日
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
Sabri Çetin 2016 年 6 月 22 日
編集済み: Sabri Çetin 2016 年 6 月 22 日
Thank you for your answer, but I mean switch codes did not exist at ALL, if that was the case, how could we define them?
I do not want to make this too complicated, I want to define something, like code blocks in the Matlab's library, which appears as blue when I write it in a script file. As you know, code blocks do not depend on the context too.
Walter Roberson
Walter Roberson 2016 年 6 月 24 日
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
Sabri Çetin 2016 年 7 月 2 日
Should they have the ability to control the flow, such as if you wanted to define a "do/until" operator?
The code block in my mind controls the flow, a "do/until" operator. I would like to have 2 conditions, if the first one is true, some statements are executed, in the first is false, but the second condition holds, then some other statements are executed. In the case that none of the conditions holds, then the executions ends.
while(true)
if(p)
Statement Group 1
elseif(q)
Statement Group 2
else
break
end
end
For this, I would like to have something like the following
while(p)
Statement Group 1
elsewhile(q)
Statement Group 2
end
Walter Roberson
Walter Roberson 2016 年 7 月 2 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCode Execution についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by