Difference between "step" and "step in" in the MATLAB debugger - matlab

What is the difference between "Step" and "Step in" in the MATLAB debugger? As far as I have tested them in some debugging so far they gave me the same steps and results.

When you use step in on a function call, you will enter said function. step is used to move to the next line. When you use them on lines without function calls, they are equivalent.

The command dbstep (or the debugger tool "Step") executes the next line of code. It does not stop within that function, even if that function is implemented in MATLAB code and contains breakpoints.
The command dbstep in (or the debugger tool "Step In") also executes the next line of code, but if that line is a call to a function implemented in MATLAB code (i.e. not a built-in), it will enter that function and stop at the next line of code within the function.
If the next line is not a call to a function implemented in MATLAB code, dbstep and dbstep in are equivalent.

See the Matlab doc: dbstep
Basically, Step executes the next line, even if there is a call to complex function/other script. Step In jumps in the code on the called function if possible, else executes the entire line.

Related

step out and continue in MATLAB debugging

What is the difference between "step out (shift+F11)" and "continue (F)" in MATLAB debugger? When I debug a MATLAB function, both are triggering the cursor to come out of the loop. What is the exact difference between these two?
Continue (dbcont) will cause the program to resume execution and will only stop once it encounters another breakpoint.
Step-Out (dbstep out) will cause the currently executing function to continue and will automatically pause in the calling function regardless of whether you have a breakpoint there or not. Notice that this is referring to stepping out of a function and not a loop.
In your case, I'm assuming you only have a single function (or script) and therefore if you have no manual breakpoints after calling dbcont or dbstep out, they are going to have the same result since they will both just run the rest of the script to completion.

Error Function definitions are not permitted in this context

I have these four lines written in a file called abc.m
function [x, y] = abc(q, r)
x=q;
y=r;
end
When I press CTRL+enter, I first get a popup that says
The selected section cannot be evaluated because it contains an invalid statement
Then in the command prompt it says:
function [x,y]=abc(q,r)
↑
Error: Function definitions are not permitted in this context.
I have read the other questions on this error, and I do not understand why this simple file wont work.
When you press, ctrl+enter, this is the equivalent of Run Selection which will execute a code block surrounded by %%'s. The code in this code block is run as a script which essentially copies and executes the lines of your code block one at a time in the command window. In all current versions of MATLAB, you cannot define a function within a script or directly within the command window, which is the cause for your error.
If you simply want to execute your function, you will want to use Run (or F5) to do so which will run the entire file, as a function.

How to start a Matlab file without a function being at the top?

Say I have a file StackOverflow that has the following function on line 1:
function [hello] = hai(choice)
end
I then go to the command window and type in StackOverflow and it will run the function. However, I would like to start a timer as soon as the program begins.
That said, I get this error when I try to do so;
Function definitions are not permitted in this context
How can I run this file without the function being at the top of the document?
From the documentation of creating functions in files:
The definition statement is the first executable line of any function.
Function definitions are not valid at the command line or within a
script.
In short, you can't have a line of code before the function definition. You have two choices: you can start the timer within the function, or you can start the timer at the command line immediately before calling the function.
If you go with the second option, I suggest using shift-enter after the command to start the timer, before calling your function, so both commands are executed one immediately after the other. Or, you can just use a semi-colon after starting the timer and add the second command on the same line. In either case, both will execute when you hit enter.

Difference between script and matlab command window

I wonder what the difference is between entering a few lines in the command window, or letting a script execute them.
In the question Escape from nested try - catch statement I have an example function. I have put the selected code in a script and called it, however then it does not work properly. On the other hand, when I
select the lines and hit f9, it works as expected.
The lines are:
dbclear all
dbquit
dbstop if caught error
I call the example function as such:
dbstop if caught error
mytestmain
And the example function is:
function mytestmain
try
mytestsub
catch
end
% Definition of subfunction, may or may not be in the same .m file
function mytestsub
try
a=b; %Intentionally generate an error as b is not defined
catch
end
I think it's related to MATLAB's just-in-time (JIT) compiler, which compiles functions before it runs them.
It seems that it compiles functions differently if dbstop is set or not (see here for reference). As it currently stands, MATLAB can not recompile a function while it is run (just try saving a changed function during a dbstop, and you will get a message informing you). As you can add and remove breakpoints during a dbstop I think you can also do so programmatically, but it should be impossible to "turn on" debugging if it wasn't turned on at "compile time"
So in your cases:
Using F9 it's just pasted and parsed as if you input it manually. So first dbstop is set, then mytestmain gets compiled and executed.
Running as a script will first compile the script and mytestmain and then execute it - so dbstop would be set after compilation and therefore not in effect.
Depending on what you mean by "doesn't work", it could just be because the debugger is a special context and certain debugger commands - dbup, dbdown, and dbquit - only work when you're at a debugger "K>>" prompt. Once you call a script, you're no longer at the debugger prompt but in normal code execution - inside a nested M-code call stack - and they just don't work there. When you F9, it does the lines individually, so each one is done from the prompt.
As a workaround, if you really want to execute a sequence of debugger commands like this, you could write a little Java Swing widget to enter the text in to the command window just as though you were typing it in.

undefined variable in MATLAB

I am newbie to MATLAB .I have written a code to upsample a data.when executed always shrows the particular error(below)
??? Input argument "n" is undefined.
Error in ==> upsamp at 7
mm=min(n)
but when i just write the foll. output [n1,y]=upsamp([1,2,3,4,5,6],-1:4,3) command window ,its shows me the correct upsampled data with its figure.
then why the error is popping up? Or i just click on run button,and error is shown in command window.
Please help me out to debug that error:
My code is
function[n1,y]=upsamp(n,x,I)
mm=min(n)
mx=max(n)
n1=mm*I:(mx*I+I-1)
x1=x'
x1=[x1,zeros(length(x),I-1)]
x1=x1'
y=(x1(:))'
subplot(2,1,1)
stem(n,x)
title('original sequence ')
xlabel('Range')
ylabel('sequence')
subplot(2,1,2)
stem(n1,y)
title(' unsampling')
xlabel('Range')
ylabel('sequence')
end
As others have noted, if you want to run a function that takes input arguments, you have to call it manually from the command prompt with any required arguments.
Otherwise, if you like to use the Run button (F5) from the editor, consider creating a run configuration (they can be used in smart ways)
The "run" button is only for scripts (i.e. just a plain list of statements without "function" at the top). This is a function, so it should only get called from the matlab command line like you described.
The run button calls your function with no arguments.
Since arguments to your function are non-optional, you get an error.
Either call your function from the interactive Command Window, or write a short script that provides appropriate arguments, and use the Run button with that script. You can still single-step into your function.