Matlab Script and Function Quiz
40 comprehensive questions on MATLAB script files and function programming, covering .m file structure, input/output parameters, variable scope management, anonymous function creation, and documentation techniques — with 16 code examples to master MATLAB's functional programming capabilities.
Question 1
What is the file extension for MATLAB script and function files?
Question 2
How do you create a simple MATLAB script file?
function myScript
% My first script
disp('Hello, MATLAB!')
endQuestion 3
What is the difference between a script and a function in MATLAB?
Question 4
How do you define a function with multiple inputs and outputs?
function [out1, out2] = myFunction(in1, in2)
out1 = in1 + in2;
out2 = in1 * in2;
endQuestion 5
What happens to variables created inside a function?
function result = computeSum(a, b)
temp = a + b;
result = temp * 2;
endQuestion 6
How do you declare a global variable in MATLAB?
global myGlobalVar
myGlobalVar = 42;Question 7
What is an anonymous function in MATLAB?
square = @(x) x.^2;Question 8
How do you add help documentation to a function?
function result = myFunction(x)
%MYFUNCTION Computes something with input x
% RESULT = MYFUNCTION(X) processes the input
% and returns a result.
%
% See also OTHERFUNCTION
result = x * 2;
endQuestion 9
What is the purpose of the nargin and nargout functions?
function result = flexibleFunction(varargin)
if nargin == 1
result = varargin{1} * 2;
else
result = sum(cell2mat(varargin));
end
endQuestion 10
How do you handle variable number of inputs using varargin?
function result = sumAll(varargin)
result = sum(cell2mat(varargin));
endQuestion 11
What is function precedence in MATLAB?
Question 12
How do you create a function handle?
f = @sin;
result = f(pi/2);Question 13
What is the difference between local and global variables?
Question 14
How do you use anonymous functions with multiple statements?
Question 15
What does the exist() function check?
if exist('myVar', 'var')
disp('Variable exists');
endQuestion 16
How do you create nested functions in MATLAB?
function outerFunction(x)
function innerFunction(y)
disp(y);
end
innerFunction(x);
endQuestion 17
What is the purpose of the persistent keyword?
Question 18
How do you handle errors in MATLAB functions?
Question 19
What is a subfunction in MATLAB?
Question 20
How do you pass functions as arguments to other functions?
function result = applyFunction(func, data)
result = func(data);
end
% Usage:
result = applyFunction(@sin, pi/2);Question 21
What is the significance of function input validation in MATLAB?
Question 22
How do you create a function that returns multiple values conditionally?
Question 23
What is the role of comments in function documentation?
Question 24
How do you optimize function performance in MATLAB?
Question 25
What is function overloading in MATLAB?
Question 26
How do you create recursive functions in MATLAB?
function result = factorial(n)
if n <= 1
result = 1;
else
result = n * factorial(n-1);
end
endQuestion 27
What is the difference between scripts and functions regarding variable scope?
Question 28
How do you handle optional function parameters?
function result = processData(data, threshold)
if nargin < 2
threshold = 0.5; % default value
end
result = data > threshold;
endQuestion 29
What is a function workspace in MATLAB?
Question 30
How do you debug MATLAB functions effectively?
Question 31
What is the purpose of the feval() function?
funcName = 'sin';
result = feval(funcName, pi/2);Question 32
How do you create function templates or boilerplate code?
Question 33
What is the significance of function naming conventions in MATLAB?
Question 34
How do you handle large datasets in MATLAB functions?
Question 35
What is the role of assertions in MATLAB functions?
function result = divideNumbers(a, b)
assert(b ~= 0, 'Division by zero');
result = a / b;
endQuestion 36
How do you create modular code using functions?
Question 37
What is the impact of function call overhead in MATLAB?
Question 38
How do you ensure function portability across different MATLAB versions?
Question 39
What is the relationship between functions and the MATLAB path?
Question 40
Considering MATLAB's function system as a whole, what fundamental programming principle does it enable that transforms interactive computing into structured development?
