MATLAB Environment and Syntax 3 — Questions and Answers
Question 1: What does the `path` command do in MATLAB?
- Returns the current working directory
- Displays or modifies the MATLAB search path (Correct answer)
- Lists files in the current folder
- Sets the default save location for files
Correct answer: Displays or modifies the MATLAB search path
The `path` command displays the current MATLAB search path and can be used to add or remove directories from it.
Question 2: In MATLAB, what is the difference between a script and a function file?
- Scripts can accept input arguments; functions cannot
- Functions have their own workspace; scripts share the base workspace (Correct answer)
- Scripts must end with `.fun`; functions end with `.m`
- There is no difference
Correct answer: Functions have their own workspace; scripts share the base workspace
Functions operate in their own local workspace and can accept inputs/outputs, while scripts run in the base workspace.
Question 3: What does `format long` do in MATLAB?
- Sets line width to 80 characters
- Displays numbers with 15 significant digits (Correct answer)
- Formats output as a long integer
- Enables long variable names
Correct answer: Displays numbers with 15 significant digits
`format long` switches numeric display to 15 significant digits instead of the default 5 (`format short`).
Question 4: How do you add a comment to a MATLAB script?
- Use // before the comment text
- Use # before the comment text
- Use % before the comment text (Correct answer)
- Wrap the text in /* */
Correct answer: Use % before the comment text
The percent sign `%` begins a single-line comment in MATLAB; everything after it on that line is ignored.
Question 5: What is the output of `class(uint8(5))` in MATLAB?
- double
- int
- uint8 (Correct answer)
- numeric
Correct answer: uint8
`class()` returns the data type of a variable as a string, so `class(uint8(5))` returns `'uint8'`.
Question 6: Which MATLAB function opens the built-in documentation browser for a specific function?
- help
- doc (Correct answer)
- info
- man
Correct answer: doc
`doc functionName` opens the full HTML documentation page in the MATLAB Help browser, while `help` shows text-only docs in the Command Window.
Question 7: What is the MATLAB operator for element-wise multiplication of two arrays?
- *
- .* (Correct answer)
- x
- .x
Correct answer: .*
The `.*` operator multiplies arrays element by element, whereas `*` performs matrix multiplication.
What does the `path` command do in MATLAB?