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

72 次查看(过去 30 天)
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 个评论
EFB
EFB 2015-2-7
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 2015-2-7
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.

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2015-2-7
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 2015-2-19
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 个评论
Steven Lord
Steven Lord 2017-1-25
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 2017-1-25
编辑:Image Analyst 2017-1-25
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by