Running a function script

조회 수: 55 (최근 30일)
Niels Uitterdijk
Niels Uitterdijk 2019년 1월 8일
댓글: Niels Uitterdijk 2019년 1월 8일
Hi there,
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.

답변 (3개)

OCDER
OCDER 2019년 1월 8일
편집: OCDER 2019년 1월 8일
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
>> runMain
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.
  댓글 수: 2
madhan ravi
madhan ravi 2019년 1월 8일
Niels Uitterdijk's answer moved here for consistency:
Thanks for your reply.
I am doing perfectly fine with debugging. Problem is that once I solved a particular bug, my editor is still at the line of the debug. When I then press F5 it runs the function directly, which is not possible.
Ideally when I press F5 on the function it'd run the main file.
It's just conveniency really but I couldn't stop wondering whether this was possible.
OCDER
OCDER 2019년 1월 8일
Oh, in that case, Steven's and TADA's answer is probably what you're looking for.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2019년 1월 8일
See this documentation page for instructions on how to customize what gets run when you press the green triangle Run button. I believe this is also what gets used when you press F5.

TADA
TADA 2019년 1월 8일
In Fact You Can Do It.
Open The Popup Menu Under Your Run Button At The Menu Strip.
You Can Type Whatever Code You Want To Run Right There, So It Could Be main, Or That Function You're Debugging With Whatever Input You WantIMG_20190108_171216.jpg
  댓글 수: 1
Niels Uitterdijk
Niels Uitterdijk 2019년 1월 8일
Awesome! Thank you a lot!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by