Find an unsuppressed line in a large script - matlab

I am running a script which is fairly large. In the output screen I get ans = 10.
The problem is that the code is very large and I cannot pin point where this output is coming from.
Are there any tips to find the origin of this output within the MATLAB environment since I prefer not to have a random output on the screen?

In the case of a single script file, you can programmatically invoke mlint to return all warnings in the form of a struct:
L = mlint('my_filename'); % No need for .m at the end
Inspecting the structure you could see the following:
This structure has a field named 'message' which contains various issues, among which is what we're after - 'Terminate statement with semicolon to suppress output (in functions).'. At this stage you can run
find( strcmp({L.message},... % output is not suppressed on purpose
'Terminate statement with semicolon to suppress output (in functions).') )
Then inspect the line numbers that get printed.
Alternatively, if you want to skip the variable editor, or want to inspect a whole folder of .m files, you can run mlintrpt (in the case of a folder) or mlintrpt('plotWithoutOutliers') (in the case of a single file) and get a report in the following form:

As Luis said: look for the mlint error. In code this is shown as an orange shaded = and orange wiggle beneath it; on the scroll bar on the right it shows an orange line which you can hover over to see what the warning is and click on to go to the warning.
Additionally I included the red errors for completion. These will cause your code to be unable to run. Same here, red wiggle + red line on the right.
Last, the square on the top right, by the arrow, will either be green (no problems), orange (warnings, but the code will be able to run usually) or red (code won't be able to run).

Used Dev-iL's answer to create a simple function that will tell you the lines of code that are not suppressed. This way you don't need to inspect the returned lines in the structure L. Example: lines = UnsuppressedLines('isEven.m')
function [lines] = UnsuppressedLines( matlabScript)
l = mlint(matlabScript);
unsuppressedInstances = find( strcmp({l.message},'Terminate statement with semicolon to suppress output (in functions).') )
if isempty(unsuppressedInstances)
fprintf('No unsuppressed lines in script')
lines = [];
else
lines = {l.line};
lines = lines(unsuppressedInstances);
end
end

Related

How can I go to red line of error in Eclipse?

My question is if there is any command that allows me to go to the red error line in Eclipse. For example, I am in line 1 of my file and there is an error in line 1200 – is there any way to go directly to that line? (Obviously, I don't know the line number, so I don't use ctrl + L.)
As the commenter said, you can do this from the problems view, but there are ways to navigate somewhat efficiently in the source view.
While your cursor is in a file in the source view, the function "next" might be bound to "Ctrl+." (control-period). This is implemented in various scopes to do different things, but in the source view, it goes to the next "issue" in the file. If your only issue in the file is the error, then that keypress will bring you to that line.
In addition, the right-hand margin of the source view contains a column that spans the entire file. If there is an error on a line, you'll see a red horizontal block in that column. The height of the block would likely be relative to the size of the file. You can click on points in that column to go to that portion of the file, so if you click on the red mark, it will land close to that line.

MATLAB error using "diary"

I'm learning MATLAB and for my homework am supposed to use the diary feature to save a file from the command window. I used the following code,
%% 2.21
clc
clear
diary( 'degrees.dat' )
columnOne = linspace(0, 180, 8);
columnTwo = columnOne .* (pi / 180);
D_to_R = [columnOne', columnTwo']
diary off
clc
clear
load ( 'degrees.dat' )`
and got the error:
Error using load
Number of columns on line 3 of ASCII file
degrees.dat must be the same as previous
lines.
I put the above code in the editor window but tried putting it directly in the command window and it didn't make a difference. The code up until loading the saved file seems to work fine and I can't see a difference in number of columns like the error indicates.
Any ideas?
You are using diary correctly. However, your use of load is incorrect, and most likely unneeded based on the problem at hand. You've shown that you can save a file with 'diary'.
If you want to show the contents of your diary as stored in the file degrees.dat in the command window you can type, into the command window:
type degrees.dat or type('degrees.dat').
Similarly, if you want to open it in the edit window you can use
edit('degrees.dat') or edit degrees.dat

Why am I getting this error when selecting files with uigetfile?

I used uigetfile to select multiple images. When I select the images and push the open button or press the enter key everything is okay. But when I instead select the images and double click on the selected images, I get this error:
Cell contents reference from a non-cell array object.
Error in Picketfence>insertpb_Callback (line 141)
file = fullfile(PathName,FileNames{i});
Here's my code:
c={'*.*', 'All Files(*.*)';'*.jpeg','figure (*.jpg)';'*.tif',...
'figure (*.tif)'};
[FileNames,PathName] = uigetfile(c, 'Select Images','MultiSelect','on');
if char(FileNames)
nfiles = length(FileNames);
handles.profile = zeros(1024,1024);
for i = 1:nfiles
file = fullfile(PathName,FileNames{i});
handles.profile = handles.profile+im2double(imread(file));
end
end
Why am I getting this error and how can I fix it?
The problem is that you can't select multiple files by double-click. When you select your files, then double-click on one of them, what's actually happening is that you are selecting only the one you double-clicked on. In other words, the first click of the double-click selects just that one, clearing the others.
When the GUI closes and returns to your code, you only have a single file selected, so FileNames isn't a cell array, just a character string. This is why the cell content indexing with {} fails.
A few more points about your code...
Your conditional check if char(FileNames) is wrong. The char function doesn't return logical (i.e. boolean) values. It converts things to character arrays. As per the documentation for uigetfile, the outputs will be zero when the selection is cancelled or the GUI closed, so the appropriate check in your case would be:
if ~isequal(FileNames, 0)
% Do your processing here...
else
% Nothing was selected; take some other action
end
You may want to account for the possibility of only 1 file being selected, thus having FileNames be a character array. The simplest way to do this is to first check if FileNames is a character array using ischar, and if so encapsulate it in a 1 element cell array (since your code expects cell arrays):
if ischar(FileNames)
FileNames = {FileNames};
end
Then you can do all your processing as you have it written above.

Matlab imline and text

Thank you for your answer. I should have been more clear. I need a text object for every line. Here's how far I got:
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy = imline;
addNewPositionCallback(xy,#(pos) disp_slope(pos));
end
function disp_slope(pos)
delete(findobj(gca,'Type','text'));
text((pos(1)+pos(2))/2,(pos(3)+pos(4))/2,['\DeltaY/\DeltaX = ',num2str((pos(4)-pos(3))/(pos(2)-pos(1))),...
' [\DeltaX = ',num2str(pos(2)-pos(1)),', \DeltaY = ',num2str((pos(4)-pos(3))),']']);
So, each toggle on the toggle button in the figure will throw in a draggable/resizable line and as I move the line the slope shows and updates, which looks pretty close to what I want. There are, however, two issues:
It deletes all other text boxes except for the very last (current)
text box of the line I'm moving around. I want to keep the last
value remained and shown for all existing lines.
If I delete a line by right-clicking on the line and choose
"delete", it deletes the line, but not the text box and rightfully
so. Now I have a text box that shows the slope of the line that
exists no longer in the figure. I want the text box to disappear
along with the line.
I'm having these problems and really not going anywhere because imline object behaves so much different than other typical objects and also the concept of the addNewPositionCallback is quite convoluted.
Please, somebody enlighten me on this.
Many thanks in advance,
Eric
Previous message:
I create a figure with toggle button on its toolbar by uitoggletool. The callback function for it is shown below:
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy=imline;
addNewPositionCallback(xy,#(xy)...
title(['\DeltaY/\DeltaX = ',num2str((xy(4)-xy(3))/(xy(2)-xy(1))),...
'[\DeltaX = ',num2str(xy(2)-xy(1)),...
',\DeltaY = ',num2str((xy(4)-xy(3))),']']));
end
As you can see, the example outputs the position data in the title of a figure using the 'title' command.
Is there a way to output this in a text box using the 'text' command?
I want to display the slope next to the line drawn.
Also, it will be great if the text box also gets deleted together with the associated line.
Please, help.
Thank you,
Eric

comment out an interval in matlab through command line

I have a very long script in MATLAB (1500 lines) and want to test two different settings. To do so, I need to comment out some codes in a specific interval (e.g. form line 234 to line 255).
Is there a function in MATLAB that takes the intervals and comments/uncomments them automatically?
You can just highlight your code and click on the comment button. Highlight and click on uncomment to remove comment.In windows you may also use shortcut keys cntrl+r and cntrl+t. But yeah, the if else is a better idea but takes more time in the beginning to code in the if else.
I would agree with others that putting your code into a block surrounded by if-else would probably be a better solution than what you originally asked for.
But if you want to do it, you can use the following function:
function commentout(fromline, toline)
currentDoc = matlab.desktop.editor.getActive;
currentDoc.insertTextAtPositionInLine(sprintf('%%{\n'), fromline, 0)
currentDoc.insertTextAtPositionInLine(sprintf('%%}\n'), toline+1, 0)
This will work in most recent versions of MATLAB.
To uncomment, I think you'll need to do something a bit more complex, like getting the entire text from the active document, removing the specified comment lines, then setting the entire text back again (get, modify and set the Text property of the document).