Course Content
Matlab From Beginner to Professional
Matlab From Beginner to Professional
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.
- Items in orange are all required by Matlab;
- The
function
declares the command to create a function; - The output variables are enclosed in square brackets followed by an equals sign;
- The input variables are enclosed in parentheses after the function name;
- Items in blue are to be filled in:
- A comma-separated list of the output variables;
- A comma-separated list of the input variables;
- The name of the function (following the same conventions as variable names);
- The periods in grey are simply used to make the code look nicer. Matlab requires there be no spacing or line breaks between these elements, and in this case we can use periods (which indicate a continuation of code to the next line) to space things out. Otherwise, the code can get quite crammed, particularly when the lists of output and input variables get long;
- Other notes:
- Functions with no output variables can omit the square brackets and equals sign (see the snippet);
- Functions with no input variables can omit the parentheses (see the snippet);
- Functions with neither can omit both (see 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!