How do you write comments in sub functions?

2 次查看(过去 30 天)
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 个)

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

Community Treasure Hunt

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

Start Hunting!

Translated by