Variables and Workspace Interaction Flashcards
7 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 7 Variables and Workspace Interaction flashcards as text
What is the key difference between `global` and `persistent` variables in MATLAB?
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.
What does `inputname(n)` return inside a MATLAB function?
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.
How can you list all variables in the workspace that match the pattern 'data*'?
Answer: Both A and B
Both `who data*` and `whos('data*')` accept wildcard patterns and list matching workspace variables.
After running a script that sets `x = 42`, the variable `x` persists in the base workspace. Why?
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.
What does `evalin('base', 'x')` do?
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.
Which command writes a variable `result` from within a function into the base workspace?
Answer: assignin('base', 'result', result)
`assignin('base', 'result', result)` places the variable into the base workspace from within a function.
What is the purpose of the `pack` command in MATLAB?
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.