Clear Filters
Clear Filters

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

77 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!
EFB on 7 Feb 2015
It appears that this "bug" happens when I run with Command + Enter, but not when I run by choosing the run button on the ribbon. So then I just need to figure out how to find a shortcut for this. (At + Command + R does not work, perhaps because I have a Norwegian keyboard).
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:
% 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);

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.
Steven Lord
Steven Lord on 25 Jan 2017
If you type a command into the Command Window and that command throws an error, what "line number" should be displayed? When you evaluate a selection or a section of code, what's happening is in some ways similar to that.
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:
% 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);

Sign in to comment.


Find more on Desktop in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!