Skip to content
Snippets Groups Projects
Commit ed2fcd9f authored by Max Johansson's avatar Max Johansson
Browse files

commit mscommit msg

parent 4cc13319
No related branches found
No related tags found
No related merge requests found
...@@ -266,6 +266,21 @@ x=3; ...@@ -266,6 +266,21 @@ x=3;
longName = 5; longName = 5;
x = 3; x = 3;
% ctrl + I is a nice shortcut
for j = 1:length(x)
if j < 3
y = 4;
end
end
% Commenting large sections at once with ctrl + R, uncomment with ctrl + T
for j = 1:length(x)
if j < 3
y = 4;
end
end
%% Path %% Path
% Lastly, the matlab path % Lastly, the matlab path
......
clc
clear
%% Debugging
% There are always things that go wrong when programming.
% Using the matlab debugger efficiently is important.
% The debugger works automatically by running code with breakpoints
% for example:
x = 5;
y = 3;
z = x + y;
z = z + y;
z = z + y;
z = z + y;
z = z + y;
z = z + y;
z = z + y;
% Setting a breakpoint at z = x + y allows us to pause the program and
% step through the code line by line
% The active line is shown by the green arrow
%
% Now we can step through line by line by clicking Step (F10)
% The workspace is updated as we work through the code.
%
% We have multiple options here, step and continue etc...
% If we choose continue, matlab keeps running code until the next
% breakpoint, or if we hit the same breakpoint again
%% Breakpoints
% The debugger stops at breakpoints. In the breakpoints menu we can
% select more advanced debugging tools, for example conditional breakpoints
%
% 1. Mark a certain line and press "Breakpoints -> Set condition"
% 2. If we set x > 3 at line 36, the debugger will stop
x = 4;
z = x + 3;
% 3. Try instead to set x > 5
x = 4;
z = x + 3;
% 4 . We see that the debugger ignores the breakpoint
% Right clicking a breakpoint gives useful options
%
% Conditional breakpoints can be used efficiently when debugging
% loops, recursion and so on
%% Debugging external function
% We can use breakpoints in an external function as well.
x = 3;
y = myFunctionFile(x);
% If we are in debugging mode, we can choose to step into or over
% functions
x = 3;
y = myFunctionFile(x);
%% Running code in debug mode
% If we don't know where we want to stop running a certain program, we
% can select "Run -> Pause on Errors".
% This pauses code when an error is met, and backpedals one step
% This does now work for "run selection" unfortunately
x = [1,2];
y = [2;1;4]
x*y
% This mode works for external functions as well.
function y = myFunctionFile(x)
% The debugger stops inside the function.
% Very useful since we now have access to all the function inputs
% in the workspace!
y = x + 3;
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment