Is it possible to leave an โ€œendโ€ in MATLAB functions?

I am completely new to MATLAB programming, but got a script that somehow does not account for all the end statements for function s.

For instance:

 function pushbutton_open_Callback(hObject, eventdata, handles) [FileName,PathName,FilterIndex] = uigetfile('*.txt','Select the CONFIG file'); if FileName~=0 init_session(hObject, FileName, PathName); end % shouldn't there be an "end" here? function pushbutton_start_Callback(hObject, eventdata, handles) % .... 

Is this site good? Apparently there are no syntax errors when I try to run it, and the program worked whenever we used it. Are functions executed automatically until the next function statement?

+4
source share
3 answers

I would suggest that in typical MATLAB code this is more common than not leaving end s. This is not a problem, but if you also want to add them to this fine. I would not say that it is a bad style to make any choice (FWIW, I usually leave them).

There are some circumstances in which they should be, for example:

  • If you write object-oriented code, the method functions must have end
  • Nested functions must have end
  • If any function or subfunction in the file has end , they all must.

Since the typical part of simple MATLAB code basically has one function for each file, possibly with some sub-functions, has no nesting function and is not object oriented, it will basically leave end s.

+10
source

This is completely normal, Matlab understands that the end of the file is the end of the function. However, when you have several (nested) functions in one file, you must write end .

For instance:

 function y=f(x) y=x^2+g(x) function y2=g(x2) y2=2*x2; end end 
+4
source

Yes, although this is considered bad style (pedantic), some people prefer not to have extra lines in their code.

+1
source

Source: https://habr.com/ru/post/1385890/


All Articles