Skip to main content
Social Sci LibreTexts

13.4: A2.4 Deciphering Matlab’s Error Messages

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    As noted in the previous section, Matlab’s error messages are often helpful when you’re developing a hypothesis about the cause of your problem. In this section, we’ll do some exercises that are designed to help you learn how to decipher these messages.

    Before we do the exercises, we need to divide software problems into three categories:

    • A syntax error occurs when the script contains information that is not legal Matlab code. Often, this is a result of a typo (e.g., a missing comma). When you run a script, Matlab first checks for syntax errors, and the script won’t even start running if a syntax error is detected. Instead, an error message is printed in the command window.
    • A runtime error occurs when the code, while legal in its syntax, produces some kind of problem that causes the script to terminate with an error message in the command window. For example, if the script tries to open a file with a specific name, but no file with that name exists in the PATH, the script will terminate with an error message.
    • A logical error occurs when the program runs to completion without an error message, but the result is incorrect. This usually means that the code in the script does not correctly implement the desired processing steps, but it sometimes means that the input data violate the assumptions of the script.

    Note that only syntax errors and runtime errors produce error messages. Logical errors are usually the most difficult to diagnose and solve.

    Now it’s time to try some exercises. These exercises require scripts and data in the Appendix_2 folder in the master folder:

    Exercise A2.1: A Syntax Error

    Launch EEGLAB, make Appendix_2 the current folder, and double-click on Test_Case_1.m in the Current Folder panel of the Matlab GUI. This should open the script in the Matlab editor. It’s a very simple script, but it has a bug. Click the Run button in the editor window to run the script. You should see the following error message in the Command Window:

    Error: File: Test_Case_1.m Line: 3 Column: 30
    Character vector is not terminated properly.

    This message tells you where in the script it detected a problem (Line 3, Column 30), and it tells you the nature of the problem ("Character vector is not terminated properly"). However, the nature of the problem is described in Programmerese, so you might not understand what the error message means.

    However, at least it tells you what line was running when the error message was generated (Line 3). Take a look at Line 3 of the Test_Case_1.m script. This line is trying to open a dataset named 1_N170.set. Notice that the text '1_N170.set); is underlined in red. This is Matlab’s way of telling you that it think this part of the code is a syntax error. If you hover your mouse over this part of the code, some more Programmerese pops up, saying "A quoted character vector is unterminated." Even if you don’t fully understand this message, you should be able to infer that there is some problem with the termination of the filename, 1_N170.set.

    This filename is specified as a text string, and Matlab text strings need to be enclosed in single quote characters (e.g., '1_N170.set'). Otherwise Matlab doesn’t know where the string starts and stops. In this script, we are missing the single quote that terminates the string. That’s what Matlab means when it tells you "Character vector is not terminated properly". This is a very common error that I made at least a dozen times when preparing the scripts in this book.

    Try adding a single quote mark after .set in the script. You’ll see that the red underline disappears from under this string. Now try running the script. Voila! No more error message.

    If you can’t figure out an error message in this way, try Googling it (e.g., with the search phrase matlab "character Vector Is Not Terminated Properly"). When I did this, I found a page on that proposed a good solution.

    Exercise A2.2: A More Subtle Syntax Error

    Close Test_Case_1.m and double-click on Test_Case_2.m to load it in the Matlab editor. Click the Run button in the editor window to run the script. You should see the following error message in the Command Window:

    File: Test_Case_2.m Line: 4 Column: 26
    Invalid expression. When calling a function or indexing a variable, use parentheses.
    Otherwise, check for mismatched delimiters.

    Take a look at Line 4 of the script. The error message says that this is an "Invalid expression" — not very informative.

    Do you see anything marked as problematic by Matlab in the editor window? If you look very closely, you’ll see that a right square bracket near the middle of the line is underlined in red, as is a right parenthesis near the end of the line. Matlab isn’t sure which one of these is the source of the problem, so you now have two hypotheses to examine.

    When there are problems with square brackets or parentheses (which Matlab calls “delimiters”), this usually means that they’re not paired properly. That is, every left bracket needs a corresponding right bracket. To fix a problem like this, you need to make sure that each delimiter has a pair and that both delimiters are in the correct places. If you look closely at Line 4, you’ll see that there is a left parenthesis that is correctly paired with the underlined right parenthesis.

    But if you look at the square brackets, you’ll see that there are two right brackets and only one left bracket. This means you need to figure out if we are missing a left bracket or if we have a right bracket that shouldn’t be there. In this case, the problem is an extra right bracket. Try deleting the underlined right bracket. You should see that the red underlines disappear, including the one under the right parenthesis. Now try running the script. It should now run properly.

    Exercise A2.3: A Simple Runtime Error

    Close Test_Case_2.m, double-click on Test_Case_3.m, and click the Run button to run the script. You should see the following error message in the Command Window:

    Unrecognized function or variable 'EGG'.
    Error in Test_Case_3 (line 4)
    [ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EGG, 0 );

    This error message is actually reasonably easy to understand: There is a variable named EGG on Line 4 that isn’t recognized. There are usually two possible explanations for this kind of problem. The first is that the variable is defined in the wrong part of the program, so it hasn’t yet been defined when Matlab tries to execute Line 4. You can just search for the variable to see if it’s defined later. The second common explanation is a simple typo. In this case, it’s pretty clear that variable is supposed to be named EEG, not EGG. If you change the name to EEG and run the script, you’ll find that the problem has been eliminated.

    Note that Matlab doesn’t underline any of the code in the script when it detects this error. That’s because it’s a runtime error rather than a syntax error. In other words, Line 4 is perfectly legitimate Matlab code when considered in isolation. It’s only a problem in the context of the rest of the code (i.e., because no variable named EGG has been defined on lines that execute before Line 4).

    Exercise A2.4: A Slightly More Complicated Runtime Error

    Close Test_Case32.m, double-click on Test_Case_4.m, and run the script. You should see the following error message in the Command Window:

    Error using load
    Unable to find file or directory
    Error in pop_loadset (line 139)
                TMPVAR = load('-mat', filename);
    Error in Test_Case_4 (line 7)
    EEG = pop_loadset('filename', Dataset_filename );

    This error message is a little more complicated. The script calls an EEGLAB function named pop_loadset, and Matlab actually detected the error while pop_loadset was executing. The error message therefore tells you what line of pop_loadset was running when the error was detected, along with the line of Test_Case_4.m that called pop_loadset.

    When you get an error message like this, it probably means that you have sent some kind of invalid data to the pop_loadset function. It’s possible that there is a bug in an EEGLAB or ERPLAB function that you are calling, but it’s much more likely that the problem is originating in your script. However, if you can’t figure out the problem by looking at your script, you might want to open the script for the function that your script is calling. You can do that by selecting the name of the function within your script, right-clicking on it, and selecting Open “pop_loadset”.

    I’m not actually going to reveal the problem with Test_Case_4.m here. That will be revealed in the next section.

    This page titled 13.4: A2.4 Deciphering Matlab’s Error Messages is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Steven J Luck directly on the LibreTexts platform.