Running a function script
6 views (last 30 days)
I am writing a function in a seperate script. While debugging I keep hitting F5 to test it, which obviously gives an error as a function cannot run by itself. Now I know the easy option is to stop being stupid and run the main file but I was wondering whether there's some way to get around it and call the main file when I press F5 while I'm typing in the function file.
OCDER on 8 Jan 2019
Edited: OCDER on 8 Jan 2019
To debug a function that is part of a larger set of codes, perhaps you should use debugging features such as code breaks and dbstop if error. This is an example workthrough for debugging complex codes:
>> runMain % your main file
Error: Something went wrong in functionA line 145 etc etc
>> dbstop if error
If your code runs into an error, your editor will open to the error line to help you debug. You'll need to traverse the Function Call Stack to find the function that really needs the fix. For instance, if functionA calls functionB with incorrect inputs, and functionB throws the error message, the dbstop will stop in functionB when you actually need to fix functionA. Go to Function Call Stack > functionA.m to debug functionA.
With dbstop, the local variables are given to you so you can figure out what's the issue.