How to define user-defined code blocks MATLAB?
조회 수: 15 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Steven Lord
2016년 6월 22일
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
2016년 6월 21일
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!