How can i define multple syntax for my function?

2 visualizzazioni (ultimi 30 giorni)
I would like to automatically create documentation for my MATLAB function using live function, so i can call
doc function_name
to show the generated doc, like a real MATLAB function. How can i define multiple way of calling my function?
My function is:
function [x, output, grafico] = bisezione(f, x0, TOL_utente, NMAX_utente)
x, f and x0 are needed for my function to work, while output, grafico, TOL_utente and NMAX_utente are optional. I would like to show all the possible syntax (like in the fzero MATLAB function reported in the image), but generating the live function only allows me to put one function for each file. Is there a solution?Schermata 2019-04-03 alle 21.54.10.png
end

Risposte (2)

Walter Roberson
Walter Roberson il 3 Apr 2019
You only define the function once: you just put multiple lines in the initial contents so that the different possibilities are shown to the user when they use help.
You examine the number of inputs with nargin() or you use exist('options','var') to see if any value was passed in. See also https://www.mathworks.com/help/matlab/ref/inputparser.html for more complicated cases.
if nargin < 2
error('Need at least 2 parameters')
end
if nargin < 3 || isempty(TOL_utente)
TOL_utente = 1e-7; %default value
end
if nargin < 4 || isempty(NMAX_utente)
NMAX_utente = 100; %default value
end

Daniele Micera
Daniele Micera il 3 Apr 2019
Modificato: Daniele Micera il 3 Apr 2019
I still don't get it. I already managed various cases in my code just as you did using nargin. I defined my function just once in the way i showed above, and i don't want to use help function_name, i want to use doc function_name. I'll post some pictures of my function,
live function generated by right-clicking on bisezione.m and selecting "open as live function" and the output i get when I type
doc bisezione
in the command window.
In the command window, typing
[x output grafico] = bisezione(f, x0, TOL_utente, NMAX_utente)
where TOL_utente and NMAX_utente are optional and user defined parameters, or typing
[x output grafico] = bisezione(f, x0)
are both accepted as valid command, because I managed them in my code with nargin as showed.
What I want to do is showing various combination of syntax in the documentation like the fzero function.
Obviously, the code showed in the first picture isn't complete, i just wanted to show how i wrote the declaration of the function.

Community Treasure Hunt

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

Start Hunting!

Translated by