MATLAB Programming and Scripting 3 — Questions and Answers
Question 1: Which MATLAB function converts a string to a number?
- num2str
- str2double (Correct answer)
- char2num
- string2num
Correct answer: str2double
str2double converts a string representation of a number to a double-precision value.
Question 2: What is a cell array in MATLAB primarily used for?
- Storing only numeric values efficiently
- Storing heterogeneous data of different types and sizes (Correct answer)
- Fast matrix operations
- Holding only character arrays
Correct answer: Storing heterogeneous data of different types and sizes
Cell arrays can hold elements of different data types and sizes, unlike standard numeric arrays.
Question 3: In a MATLAB script, what does 'break' do inside a for loop?
- Skips to the next iteration
- Exits the loop immediately (Correct answer)
- Exits the entire script
- Pauses execution temporarily
Correct answer: Exits the loop immediately
'break' immediately terminates the innermost loop and transfers control to the statement after it.
Question 4: Which syntax correctly accesses the element in row 2, column 3 of matrix M?
- M[2,3]
- M(2,3) (Correct answer)
- M{2,3}
- M.2.3
Correct answer: M(2,3)
MATLAB uses parentheses with comma-separated indices to access matrix elements.
Question 5: What does 'varargin' allow in a MATLAB function definition?
- Variable number of output arguments
- Variable number of input arguments (Correct answer)
- Variadic return types
- Argument name aliasing
Correct answer: Variable number of input arguments
'varargin' collects any number of additional input arguments into a cell array.
Question 6: Which MATLAB function checks if a variable exists in the current workspace?
- exist() (Correct answer)
- isvar()
- hasvar()
- defined()
Correct answer: exist()
exist('varname','var') returns 1 if the variable exists in the current workspace.
Question 7: What is the difference between '==' and 'isequal()' in MATLAB?
- No difference — they always return the same result
- '==' does element-wise comparison returning an array; isequal() returns a scalar true/false (Correct answer)
- isequal() works only on strings
- '==' works only on scalars
Correct answer: '==' does element-wise comparison returning an array; isequal() returns a scalar true/false
'==' returns a logical array of element-wise comparisons, while isequal() returns a single logical value indicating whether inputs are identical.
Which MATLAB function converts a string to a number?