필터 지우기
필터 지우기

How do you write comments in sub functions?

조회 수: 2 (최근 30일)
Bryan
Bryan 2017년 11월 22일
댓글: Stephen23 2017년 11월 22일
So I'm given a homework problem in my Introductory Matlab class, where I'm suppose to write my own function about a rocket. My function has two sub functions called gravity and thrust. So my professor requires us to write comments describing our function, using the % command. I'm having trouble figuring out where to write the commends for p2 and p3. p4 I wrote that under the sub function gravity, and for p5 under the sub function for thrust. But when I try to write a comment for p2 or p3, it runs this in the command window:
'gravity not found.' or 'thrust not found' .
I wrote these comments in the sub functions and the general function, but I keep getting this same answer. Does anyone know where would I write the %comments for p2 and p3. (function runs perfectly) Thanks.
p2 = evalc('help gravity');
p3 = evalc('help thrust');
p4 = evalc('help rocket>gravity');
p5 = evalc('help rocket>thrust');
  댓글 수: 1
Stephen23
Stephen23 2017년 11월 22일
Why are you using evalc for this? Why not just call help directly?

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

답변 (1개)

Guillaume
Guillaume 2017년 11월 22일
It's not exactly clear what you are calling subfunctions. I'm going to assume that you mean either a local, nested or private function.
It's also not clear what problem you are having. Comments can be written (nearly) anywhere you want. Inside or outside any function. They're just ignored by matlab. However, looking at your evalc lines, it looks like you're trying to access the help for the functions you've written.
Help is indeed written as comments and must immediately follow the function declaration, regardless the type of function.
e.g., in file rocket.m:
function something = rocket(somethingelse)
%rocket: guide the rocket to mars
%this is a normal function
end
function x = gravity(y)
%gravity: apply the effect of gravity onto the rocket
%this is a local function in the same file as rocket
end
at the command prompt:
>>help rocket
rocket: guide the rocket to mars
this is a normal function
>>help rocket>gravity
gravity: apply the effect of gravity onto the rocket
this is a local function in the same file as rocket

카테고리

Help CenterFile Exchange에서 Pulse and Transition Metrics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by