Script and Function Creation Flashcards
6 cards from real MATLAB practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Script and Function Creation flashcards as text
What is the fundamental difference in how MATLAB scripts and functions interact with the workspace?
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.
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?
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').
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'?
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.
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?
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.
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?
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.
What is a primary limitation of an anonymous function in MATLAB?
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.