General Q: Why Matlab doesn't end (close) the function when you create an object in the GUI
이전 댓글 표시
Hello All,
I just wonder why Matlab doesn't end the function (callback for instance) of any object in a GUI when created! For example, after you save the GUI, the following code will be generated in the code file without an end:
function x_callback(...)
...
% hence without an end
Another question, when you create a text field in the GUI, isn't necessary to keep the two automatically generated functions, i.e the callback & createfcn? is there a way not to have them in the code?
Thanks & Regards
채택된 답변
추가 답변 (2개)
Walter Roberson
2013년 6월 2일
8 개 추천
You are not really asking about MATLAB itself: you are asking about GUIDE, which is a tool useful to generate MATLAB code.
Image Analyst mentioned that using "end" statements is optional. That is a bit of a simplification:
- methods in objects must always use "end" (objects can only be defined in "class" files)
- nested functions must always use "end"
- if any routine in a file (including a nested function) uses "end", then all routines in a file must use "end"
- if "end" is used, then there is a difference in compilation and execution. If "end" is not used, then new variables can be "poofed" into existence at any time, including by running scripts that create variables, or by "assignin" or "evalin". When "end" is used, then a "static workspace" is created in which variables can only come into existence by way of assignment statements at that level, with poofing not being allowed; this allows for better optimization. Such workspaces also form closures
My speculation for the last couple of years has been that GUIDE does not add the "end" because if it did so, it would be necessary to re-validate the code (and possibly fix portions of it) to take into account the static workspace rules. As GUIDE was never designed really designed for efficiency anyhow, the additional efficiency and error checking of using the "end" is probably not worth the effort of building new test cases and rejigging the code (and putting in another backwards compatibility mode to allow people to use the old style.)
댓글 수: 3
Jan
2013년 6월 3일
A trailing end is not valid in older Matlab versions, e.g. in 6.5. So omitting the end might support backward compatibility.
Sean de Wolski
2013년 6월 3일
Jan's comment on backward compatibility is the main reason. The "end" is new in MATLAB 7 I believe.
Personally, as a best practice, I always use ends - even in GUIDE files.
The MATLAB Class system requires ends as well.
Walter Roberson
2013년 6월 3일
Sean, GUIDE is not currently backwards compatible to MATLAB 6, as it can generate objects that cannot be properly initialized even in some previous MATLAB 7 versions.
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!