Variables and Workspace Interaction 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 Variables and Workspace Interaction flashcards as text
Which of the following is an invalid variable name in MATLAB?
Answer: _var
MATLAB variable names must begin with a letter, followed by letters, digits, or underscores. A variable name cannot start with an underscore.
A user has several variables in the workspace: `price_usd`, `price_eur`, `tax_rate`, and `quantity`. Which command would save only the variables related to price into a file named `prices.mat`?
Answer: save('prices.mat', 'price*')
The `save` function can use a wildcard character (*) to save variables that match a specific pattern. The command `save('prices.mat', 'price*')` correctly saves all variables whose names start with 'price'.
What is the key difference between the `who` and `whos` commands?
Answer: `who` only lists variable names, while `whos` provides detailed information like size, bytes, and class.
The `who` command displays a simple list of the variables currently in the workspace. The `whos` command provides a more detailed list that includes the name, size, number of bytes, class, and other attributes for each variable.
If the workspace contains a variable `x = 10`, and you execute the command `load('my_data.mat')`, where `my_data.mat` also contains a variable named `x` with the value `50`, what will be the state of the workspace variable `x` after the command completes?
Answer: The value of `x` will be overwritten and become `50`.
When you load variables from a MAT-file, if a variable in the file has the same name as a variable already in the workspace, the `load` function will overwrite the existing variable with the one from the file.
What is the primary characteristic of a `persistent` variable in a MATLAB function?
Answer: Its value is retained in memory between calls to the function.
A persistent variable is local to the function in which it is declared, but its value is preserved in memory between calls to that function. Unlike global variables, it cannot be accessed from outside the function.
A user wants to remove all variables from the workspace but keep all loaded functions and MEX-files in memory. Which command should be used?
Answer: `clear variables`
The command `clear variables` specifically removes all variables from the current workspace while leaving other items like functions and MEX-files in memory. `clear all` would remove variables, functions, and more. `clc` only clears the command window display. `clear functions` would remove functions, which is the opposite of the desired outcome.