MATLAB Script and Function Creation Questions and Answers — Questions and Answers
Question 1: What is the fundamental difference in how MATLAB scripts and functions interact with the workspace?
- Scripts create a temporary workspace, while functions use the global workspace.
- Scripts operate in the base (or caller's) workspace, while functions have their own private, local workspace. (Correct answer)
- Both scripts and functions operate in their own private workspaces, isolated from the base workspace.
- Functions can modify base workspace variables directly without using 'global', but scripts cannot.
Correct answer: Scripts operate in the base (or caller's) workspace, while functions have their own private, local workspace.
A key distinction is that scripts execute as if you typed their commands directly into the Command Window, sharing and potentially modifying the base workspace. Functions, however, have their own isolated workspaces; data is passed in via arguments and returned via output variables, which prevents unintended side effects on the base workspace.
Question 2: A programmer creates a function that needs to accept a variable number of input arguments. Which keyword should be used in the function definition to handle this?
- nargin
- varargin (Correct answer)
- inputs{}
- arguments
Correct answer: varargin
`varargin` is a special keyword used in a function's argument list that collects all inputs from its position to the end into a cell array. This allows a function to accept any number of trailing arguments, making it ideal for optional parameters or inputs that come in pairs (e.g., 'Color', 'red').
Question 3: Which of the following correctly defines an anonymous function to calculate the square of a number and assigns it to a function handle named 'sqr'?
- function sqr(x) = x.^2;
- sqr = function(x) x.^2;
- sqr = @(x) x.^2; (Correct answer)
- anon_function sqr = (x) -> x.^2;
Correct answer: sqr = @(x) x.^2;
Anonymous functions in MATLAB are defined using the `@` operator. The syntax is `handle = @(input_arguments) expression`. This creates a function handle that stores the single executable expression.
Question 4: A user has a primary function `mainAnalysis.m`. They want to create a helper function that is only callable by `mainAnalysis` and other functions within the same file. How should this helper function be created?
- By placing it in a separate M-file named `helper.m` in the same folder.
- By defining it within the `mainAnalysis.m` file after the `mainAnalysis` function has ended. (Correct answer)
- By declaring it with the `private` keyword inside `mainAnalysis.m`.
- By creating a `private` subfolder and placing the helper function's M-file inside it.
Correct answer: By defining it within the `mainAnalysis.m` file after the `mainAnalysis` function has ended.
A function defined within the same M-file as a primary function is called a subfunction (or local function). Subfunctions are only visible and callable by the primary function and other subfunctions within that same file, providing a way to encapsulate helper routines without cluttering the main path.
Question 5: A developer needs to create a function that computes both the sum and the product of two input numbers, `a` and `b`. Which function definition syntax is correct for returning both results?
- function [sum_val, prod_val] = my_calc(a, b) (Correct answer)
- function (sum_val, prod_val) = my_calc(a, b)
- function my_calc(a, b) returns [sum_val, prod_val]
- function my_calc(a, b, sum_val, prod_val)
Correct answer: function [sum_val, prod_val] = my_calc(a, b)
In MATLAB, multiple output arguments from a function are listed in square brackets `[]` on the left side of the equals sign in the function definition line. The inputs are listed in parentheses after the function name.
Question 6: What is a primary limitation of an anonymous function in MATLAB?
- It cannot accept any input arguments.
- It must be saved in its own M-file.
- It cannot use built-in MATLAB functions in its expression.
- It can only contain a single executable statement. (Correct answer)
Correct answer: It can only contain a single executable statement.
Anonymous functions are designed for simple, inline definitions and are restricted to a single executable expression. They are not stored in separate files but are associated with a function handle variable.
What is the fundamental difference in how MATLAB scripts and functions interact with the workspace?