Built-in functions are those that come with MATLAB or are part of an add-on product. You typically don’t have source code for built-in functions and must treat them simply as black boxes. So far, you have relied exclusively on built-in functions to perform tasks in MATLAB.
Learning about built-in functions
There are many ways you can learn about built-in functions, but if you already know the name of a function, one of the simplest makes use of the help(‘function_name’) command, where function_name is the name of the function. Try it now. Type help(‘input') and press Enter in the Command window.
MATLAB does provide some types of category help. For example, type help(‘elfun') and press Enter to see a listing of elementary math functions at your disposal. When you type help(‘specfun') and press Enter, you see a listing of specialized math functions.
Sometimes the help information provided by the help() function becomes excessively long. In this case, you can use the more() function to present the information a page at a time. Before you use the help() function, type more(‘on') and press Enter to put MATLAB in paged mode. When the help information is more than a page in length, you see a
prompt at the bottom of the screen. Press the spacebar to see the next page. If you want to see only the next line, press Enter instead. When you finish reviewing help, type more(‘off') and press Enter to turn off paged mode.
Although the help() function is really useful because it displays the information you need directly in the Command window, sometimes the doc() function is a better choice. When using the doc() function, you see a nicely formatted output that includes links to example code and other information. Type doc(‘input’) and press Enter.
This is the option you should use when you want to get an in-depth view of a function rather than simply jog your memory as part of writing an application. In addition, when you find that the help() function is less helpful than you’d like, the doc() function generally provides more information.
Using help() may not always be possible because you don’t know the precise name of whatever you need to find. Another useful function is docsearch(). You use this function when you have some idea, but not a precise one, of what you need to find.
For example, type docsearch(‘input') and press Enter in the Command window. This time you see a list of potential entries to query. Notice that the input() function is still the first entry on the list, but you have a number of other choices as well.
One of the more interesting ways to search for built-in functions is to use the lookfor() function. In this case, MATLAB doesn’t look in the documentation; rather, it looks in the source code files. This kind of search is important because you can sometimes see connections between functions this way and find alternatives that might not normally occur to you.
To see how this kind of search works, type lookfor(‘input') and press Enter. Notice that the input() function is in the list, but it doesn’t appear at the top because the search doesn’t sort the output by likely candidate.
If you really want to know more about the built-in functions from a coding perspective, start with the which() function, which tells you the location of the built-in function. For example, type which(‘input') and press Enter. You see the location of this built-in function on your system.
At this point, you know that input() is found in the lang folder. However, you really don’t know what related functions might be in the same folder. Use the what() function to locate additional information about the content of the lang folder. To see this for yourself, type what(‘lang’) and press Enter.
Classes and packages are two other ways of packaging functionality within MATLAB. However, these two packaging methods provide more functionality than functions do in most cases, so it pays to look them up to see what sorts of things you can do with them. Using the doc() and help() functions provides you with information about the classes and packages.
Sending data in and getting data out
The essence of a function is that it presents you with a black box. In most cases, you send data in, it whirls around a bit, and then data comes back out. Managing data is an essential part of most functions.
Of course, some functions require only input, some provide only output, and some perform tasks other than work directly with data. For example, the clc() clears the Command window and doesn’t require any data input or produce any data output to perform the task. Every function does something; creating one that does nothing would be pointless.
The problem for many people is determining the input and output requirements for the built-in functions. The best way to discover this information is to use the help() or doc() functions. The doc() function is actually the easiest to use in this case. The input and output arguments appear at the bottom of the help screen.
To see this for yourself, type doc(‘input’) and press Enter. Scroll down to the bottom of the resulting page.
In this case, you see that the input argument is a prompt and that you must provide this input as a string. The documentation explains that the prompt is there to ask the user for a specific kind of input. The output can take two forms: an array that is calculated from the input or a string that contains the precise text the user has typed.
When you see a dual output for a function, it means that you need to tell the function what sort of output to provide or that there is a default.