MATLAB Variables and Workspace Interaction 5 — Questions and Answers
Question 1: What is the key difference between `global` and `persistent` variables in MATLAB?
- Both are shared across all functions
- Persistent variables are shared globally; global variables are local
- Global variables are shared across functions that declare them global; persistent variables retain their value between calls within a single function (Correct answer)
- There is no difference
Correct answer: Global variables are shared across functions that declare them global; persistent variables retain their value between calls within a single function
`global` shares a variable across any function that declares it global, while `persistent` retains a value between calls within one specific function.
Question 2: What does `inputname(n)` return inside a MATLAB function?
- The value of the nth input argument
- The name of the variable passed as the nth argument by the caller (Correct answer)
- The class of the nth input
- The size of the nth input
Correct answer: The name of the variable passed as the nth argument by the caller
`inputname(n)` returns a string containing the name of the variable the caller used for the nth argument, or empty if it was an expression.
Question 3: How can you list all variables in the workspace that match the pattern 'data*'?
- who data*
- whos('data*')
- list data*
- Both A and B (Correct answer)
Correct answer: Both A and B
Both `who data*` and `whos('data*')` accept wildcard patterns and list matching workspace variables.
Question 4: After running a script that sets `x = 42`, the variable `x` persists in the base workspace. Why?
- Scripts have their own isolated workspace
- Scripts share the base workspace; they do not create their own scope (Correct answer)
- The value 42 is special and always persists
- Scripts cache variables to disk automatically
Correct answer: Scripts share the base workspace; they do not create their own scope
Unlike functions, MATLAB scripts execute in the caller's (or base) workspace, so all variable assignments affect that shared scope.
Question 5: What does `evalin('base', 'x')` do?
- Evaluates 'x' in the current function workspace
- Evaluates and returns the value of 'x' from the base workspace (Correct answer)
- Clears 'x' from the base workspace
- Assigns 'x' to the base workspace
Correct answer: Evaluates and returns the value of 'x' from the base workspace
`evalin('base', expr)` evaluates an expression string in the base workspace, allowing functions to read variables from the base scope.
Question 6: Which command writes a variable `result` from within a function into the base workspace?
- assignin('base', 'result', result) (Correct answer)
- global result
- export result
- base.result = result
Correct answer: assignin('base', 'result', result)
`assignin('base', 'result', result)` places the variable into the base workspace from within a function.
Question 7: What is the purpose of the `pack` command in MATLAB?
- Compresses variables into a struct
- Defragments workspace memory by saving and reloading all variables (Correct answer)
- Packs a cell array into a matrix
- Saves variables to a ZIP file
Correct answer: Defragments workspace memory by saving and reloading all variables
`pack` consolidates fragmented workspace memory by saving all variables to disk and reloading them, which can free contiguous memory.
What is the key difference between `global` and `persistent` variables in MATLAB?