MATLAB Variables and Workspace Interaction Questions and Answers — Questions and Answers
Question 1: Which of the following is an invalid variable name in MATLAB?
- A_Var
- var_1
- _var (Correct answer)
- VarA
Correct answer: _var
MATLAB variable names must begin with a letter, followed by letters, digits, or underscores. A variable name cannot start with an underscore.
Question 2: 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`?
- save prices.mat price_usd, price_eur
- save('prices.mat', 'price*') (Correct answer)
- save prices.mat all(price)
- save('prices.mat', 'price_usd'; 'price_eur')
Correct 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'.
Question 3: What is the key difference between the `who` and `whos` commands?
- `who` lists variables and their values, while `whos` only lists variable names.
- `who` lists variables in the current function, while `whos` lists variables in the base workspace.
- `who` only lists variable names, while `whos` provides detailed information like size, bytes, and class. (Correct answer)
- `who` is an obsolete function and has been replaced by `whos`.
Correct 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.
Question 4: 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?
- The command will produce an error due to a name conflict.
- The value of `x` will remain `10`.
- A new variable `x_1` will be created with the value `50`.
- The value of `x` will be overwritten and become `50`. (Correct answer)
Correct 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.
Question 5: What is the primary characteristic of a `persistent` variable in a MATLAB function?
- It is accessible from the base workspace after the function has been called.
- Its value is retained in memory between calls to the function. (Correct answer)
- It must be initialized to a non-empty value upon declaration.
- It can be modified by other functions if they declare the same persistent variable.
Correct 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.
Question 6: 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?
- `clear all`
- `clc`
- `clear variables` (Correct answer)
- `clear functions`
Correct 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.
Which of the following is an invalid variable name in MATLAB?