Course Content
Matlab Basics
Matlab Basics
Modular Programming
Modular programming boosts computational efficiency and allows you to mix, match, and reuse useful programs that you've written. Learn how to write modular programs using functions in Matlab to save yourself time and increase the performance of your programs.
In Matlab, items highlighted in orange are required for function declarations. The function
keyword is used to define a function, with the output variables enclosed in square brackets followed by an equals sign. Input variables are placed within parentheses immediately after the function name. Items in blue need to be filled in by the user, including a comma-separated list of output variables, a comma-separated list of input variables, and the function name, which must follow the same naming conventions as variables.
The periods shown in gray are purely for formatting purposes, used to make the code visually cleaner. Matlab does not allow spacing or line breaks between elements, so periods can act as a continuation marker to distribute code more evenly across lines. This approach is especially useful when the lists of input and output variables are lengthy, preventing the code from becoming overly cramped.
Additionally, functions with no output variables can omit both the square brackets and the equals sign, while functions with no input variables can exclude the parentheses. If a function has neither input nor output variables, both can be omitted entirely, as demonstrated in the snippet.
Task
- Modulize the program you wrote for the task in Chapter 4;
- Have one program output the location of the Excel file on your computer;
- Have another program import the data;
- Have another program perform the calculations;
- Have another program export the data to Excel;
- Have another program run all these individual programs, without taking any input or output variables, and verify that it works by checking your Excel file results with your original program from Chapter 4.
Thanks for your feedback!