How to display line number in command window when error occurs for R2014b?

142 views (last 30 days)
In all the previous Matlab versions that I have used, the line number where an error occurs has always been displayed in the command window. In more recent versions of Matlab it was even possible to click on the message, and then be transported right to that line in its m-file. This is in my opinion one of the absolutely most useful features of Matlab.
Then I installed Matlab R2014b on my new Macbook Pro (Yosemite). And no line number is displayed! I only error messages such as these:
Attempted to access timestamps(2,0); index must be a positive integer or logical.
Index exceeds matrix dimensions.
>>
Please help me get back this functionality!
  2 Comments
Image Analyst
Image Analyst on 7 Feb 2015
Is it a script of a function? If it's a function, show us the first line of the file - the function definition line that lists input and output arguments.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 7 Feb 2015
I get a line number in WIndows. But try this:
try
% Some code that may generate an error.
catch ME
% Some error occurred if you get here.
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end

Ken Atwell
Ken Atwell on 19 Feb 2015
Command+Enter (equivalent to the "Run Section" button) will run just a section of code, not the entire file. In this situation, MATLAB doesn't have the full context of the file its operating in, which is why you're not getting line number information.
To run like the "Run" button, try F5 (I'm not sure why the other shortcut is not working for you). On my laptop, I actually need to press fn+F5 because a "plain" F5 is grabbed by my MacBook Pro to lower the backlighting on my keyboard.
You can always add your own keyboard shortcut; navigate to Home->Preferences->MATLAB->Keyboard->Shortcuts.
  4 Comments
Image Analyst
Image Analyst on 25 Jan 2017
Edited: Image Analyst on 25 Jan 2017
If you have your code in a script or function, the error message tells you the line number. Additionally there is the whole stack that tells you exactly where the errors happened - it "drills down". See attached code. Use it like this:
try
% Some code that might throw an error
catch ME
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s',...
mfilename, callStackString, ME.message);
uiwait(errordlg(errorMessage));
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!