MATLAB Script and Function Creation 4 — Questions and Answers
Question 1: When MATLAB searches for a function named `foo`, which location is checked first?
- The current working directory (Correct answer)
- The MATLAB built-in functions
- The user's home folder
- The path in alphabetical order
Correct answer: The current working directory
MATLAB's search order places the current working directory at the top before checking the path.
Question 2: What is a function handle in MATLAB?
- A variable that stores a reference to a function for later invocation (Correct answer)
- The first line of a function definition
- The return value of a function
- A pointer to memory where function code is stored
Correct answer: A variable that stores a reference to a function for later invocation
A function handle (created with `@`) stores a reference to a function, enabling it to be passed as an argument or called indirectly.
Question 3: How do you use `inputParser` to validate function inputs in MATLAB?
- Create an inputParser object, add parameters with addRequired/addOptional, then call parse(p, varargin{:}) (Correct answer)
- Declare input types in the function signature directly
- Use validateattributes at the function header
- Call checkInputs() before the function body
Correct answer: Create an inputParser object, add parameters with addRequired/addOptional, then call parse(p, varargin{:})
`inputParser` provides a structured way to define, validate, and set defaults for function arguments.
Question 4: What is the role of `varargin` in a MATLAB function definition?
- It allows the function to accept a variable number of input arguments (Correct answer)
- It stores the output arguments in a cell array
- It validates that inputs are numeric
- It automatically names input variables
Correct answer: It allows the function to accept a variable number of input arguments
`varargin` is a special input keyword that captures any number of additional arguments into a cell array.
Question 5: What does placing a `%` at the beginning of a line in a MATLAB script or function do?
- Makes that line a comment that MATLAB ignores during execution (Correct answer)
- Computes modulo of the line expression
- Marks the line as a breakpoint
- Divides the line output by 100
Correct answer: Makes that line a comment that MATLAB ignores during execution
The `%` symbol begins a comment in MATLAB; everything after it on that line is ignored by the interpreter.
Question 6: A nested function in MATLAB is unique because it:
- Can access and modify variables in the workspace of its parent function (Correct answer)
- Runs in the global workspace
- Cannot call any other functions
- Is automatically exported as a public function
Correct answer: Can access and modify variables in the workspace of its parent function
Nested functions share the workspace of the enclosing function, giving them access to the parent's local variables.
Question 7: What does the `error()` function do inside a MATLAB function?
- Throws an exception that stops execution and displays a message (Correct answer)
- Logs the message to a file silently
- Triggers a warning but continues execution
- Returns -1 to the caller
Correct answer: Throws an exception that stops execution and displays a message
`error()` raises an exception that terminates function execution and displays the provided message to the user.
When MATLAB searches for a function named `foo`, which location is checked first?