MATLAB Data Import and Visualization 2 — Questions and Answers
Question 1: Which MATLAB function reads a delimited text file and returns a table?
- readtable (Correct answer)
- xlsread
- load
- csvimport
Correct answer: readtable
readtable reads delimited text files (CSV, TSV, etc.) and returns a MATLAB table object with column names detected automatically.
Question 2: What does the MATLAB function 'importdata' return when loading a CSV file with a header row?
- A struct with fields 'data', 'textdata', and 'colheaders' (Correct answer)
- A plain numeric matrix only
- A cell array of strings
- A table object
Correct answer: A struct with fields 'data', 'textdata', and 'colheaders'
importdata returns a struct containing numeric data in 'data', text content in 'textdata', and column headers in 'colheaders' when the file has mixed content.
Question 3: Which colormap in MATLAB transitions from blue through green to yellow to red?
- jet (Correct answer)
- hot
- cool
- gray
Correct answer: jet
The 'jet' colormap is the classic rainbow colormap that transitions blue → cyan → green → yellow → red.
Question 4: What is the purpose of the 'hold on' command in MATLAB?
- Retains the current plot so subsequent plots are added to it (Correct answer)
- Pauses execution for a specified time
- Locks axis limits to prevent zooming
- Saves the current figure to disk
Correct answer: Retains the current plot so subsequent plots are added to it
'hold on' prevents the current axes from being cleared when a new plot command is executed, allowing multiple datasets on one axes.
Question 5: Which function creates a histogram in MATLAB?
- histogram (Correct answer)
- bar
- hist
- stem
Correct answer: histogram
The 'histogram' function (introduced in R2014b) creates histograms with automatic bin selection and returns a Histogram object, superseding the older 'hist' function.
Question 6: How do you specify that 'readtable' should treat '#' as a comment character when importing a text file?
- readtable('file.txt','CommentStyle','#') (Correct answer)
- readtable('file.txt','Comment','#')
- readtable('file.txt','Skip','#')
- readtable('file.txt','Delimiter','#')
Correct answer: readtable('file.txt','CommentStyle','#')
The 'CommentStyle' name-value pair in readtable tells the parser to ignore any line beginning with the specified character.
Question 7: What does 'xlim([0 10])' do in a MATLAB plot?
- Sets the x-axis display range from 0 to 10 (Correct answer)
- Scales data on the x-axis by a factor of 10
- Removes tick marks outside the range 0 to 10
- Sets the figure width to 10 inches
Correct answer: Sets the x-axis display range from 0 to 10
xlim sets the limits of the x-axis on the current axes to the specified two-element vector [xmin xmax].
Which MATLAB function reads a delimited text file and returns a table?