MATLAB Answers

0

Why does MATLAB terminate with no error message when using the UNIX redirection operator (<) to pass an input MATLAB file with a syntax error?

Asked by Prabhakar on 18 Jan 2011
When a MATLAB file contains a control structure that does not terminate with an 'end' statement before the end of file is reached, no syntax error message is displayed when passing the file to MATLAB via the UNIX redirection operator (<).
if 1 disp('if!\n');
else disp('else!\n');
Calling the above script 'testif.m' using the syntax below will cause MATLAB to exit without displaying either the script output or an error message.
matlab -nodisplay < testif.m

  0 Comments

Sign in to comment.

Tags

Products

1 Answer

Answer by Siddharth on 18 Jan 2011

This issue is due to the way in which MATLAB interprets files that are passed to it via the redirection operator. MATLAB executes the file by reading each line as if it were typed at the MATLAB prompt. When it reaches the end of file, it continues to wait at the command prompt for the user to enter an ‘end’ statement; however, MATLAB exits once the end of the file is reached and before an 'end' is entered. It is the equivalent of entering each line of the M-file individually at the command line, then exiting MATLAB before typing the final ‘end’ statement. No error message is displayed in this case either. To work around this issue, call MATLAB using the following syntax:
matlab -nodisplay -r testif

  0 Comments

Sign in to comment.