Tuesday, November 29, 2022
HomeMatlabCode Hint for MATLAB » Steve on Picture Processing with MATLAB

# Code Hint for MATLAB » Steve on Picture Processing with MATLAB

Right now’s submit departs from picture processing, my typical topic. As a substitute, I need to let you know about one thing that I simply put up on GitHub and the MATLAB File Alternate.

Earlier this yr, I used to be debugging some code, and I needed to know what was occurring inside a sure perform whereas the perform was operating. Sometimes, the best way to do that is to change the code by including one thing like a name to disp:

disp(“Made it to this line, and the worth of n is ” + n)

However modifying the code is commonly undesirable. It’s important to bear in mind to revive the code to its unique state when you’re performed, and also you is perhaps working with code that’s saved in a read-only location.

So, I began tinkering with methods to realize the same impact whereas leaving the code untouched, and I hit on the notion of utilizing conditional breakpoints. Whenever you set a conditional breakpoint on a code line in a file, then code execution stops at that line if an expression that you just specify evaluates to true (1). Here’s a screenshot of setting a conditional breakpoint utilizing the MATLAB Editor. Right here, I am making a breakpoint that can cease execution at line 2 of fib.m if the worth of n is 4.

Nicely, we are able to use this to exchange our use of disp if we are able to create an expression that:

• All the time returns false (0)
• Has a aspect impact of printing data to the command window
That is the idea for my Code Hint for MATLAB, which is offered on github.com/mathworks in addition to on MATLAB File Alternate. Let me illustrate it utilizing this perform fib, which is a recursive implementation of a Fibonacci quantity generator.

perform out = fib(n)

if n == 0

out = 0;

elseif n == 1

out = 1;

else

out = fib(n-1) + fib(n-2);

finish

For the best instance, I am going to add a code hint by specifying the perform title and the road quantity.

Now, a “hint” message will get displayed within the Command Window each time line 2 is about to be executed. To display, I am going to name fib:

consequence = fib(3)

In every hint message, you see the perform that is known as, fib, and the road quantity that’s about to be executed, 2. The extent of indentation varies with the depth of the decision stack, which helps us to visualise the recursive nature of this recursive perform.

For my subsequent instance, I am going to show the worth of n every time line 2 is about to executed.

consequence = fib(3)

I may show a label. For my final instance, I am going to create a number of code traces that collectively present a number of particulars concerning the perform fib because it executes.

addCodeTrace(“fib”,2,Label = “perform entry”, Expression = “n”)

addCodeTrace(“fib”,3,Label = “perform exit, returning 0”)

addCodeTrace(“fib”,5,Label = “perform exit, returning 1”)

consequence = fib(4)

Clear up.

I am to see whether or not individuals discover this convenient. Publish a remark right here in case you have ideas. Should you encounter an issue or have a suggestion, be at liberty to create a problem on the GitHub repository.
RELATED ARTICLES