Free MATLAB Data Import and Visualization Questions and Answers — Questions and Answers
Question 1: A data analyst has a CSV file named 'sensor_data.csv' with mixed data types (numbers and text) and a header row. Which function is most suitable for importing this data into MATLAB as a table, preserving the data types and using the header for variable names?
- readmatrix('sensor_data.csv')
- readtable('sensor_data.csv') (Correct answer)
- load('sensor_data.csv')
- readcell('sensor_data.csv')
Correct answer: readtable('sensor_data.csv')
The `readtable` function is specifically designed to import tabular data from files like CSVs. It automatically detects headers, parses columns with different data types, and organizes the data into a table, which is ideal for mixed data types. `readmatrix` is best for purely numeric data, `load` is for MAT-files, and `readcell` imports into a cell array without the structured benefits of a table.
Question 2: A user has imported data into a table named `patientData`. The table has variables named 'LastName', 'Age', and 'BloodPressure'. How can the user extract all values from the 'Age' column into a standard numeric vector?
- patientData('Age')
- patientData{'Age'}
- patientData.Age (Correct answer)
- patientData['Age']
Correct answer: patientData.Age
Dot notation (`tableName.variableName`) is a direct and efficient way to access the data within a single table variable, returning it as a standard MATLAB array of the appropriate data type. Using parentheses `()` would return a new table containing just the 'Age' column, while curly braces `{}` can also extract the data but dot notation is often more readable for a single variable.
Question 3: Which of the following commands will create a plot of `y` versus `x` with a red, dashed line and circular markers?
- plot(x, y, 'r--o') (Correct answer)
- plot(x, y, 'red-.*')
- plot(x, y, 'LineStyle', 'red', 'Marker', 'dashed')
- plot(x, y, 'Color', 'r', 'Line', '--', 'Marker', 'o')
Correct answer: plot(x, y, 'r--o')
The `plot` function accepts a character vector or string, often called a `LineSpec`, to define the line style, color, and marker. In 'r--o', 'r' specifies red, '--' specifies a dashed line, and 'o' specifies circular markers. The other options use incorrect syntax or combine `LineSpec` characters with Name-Value pairs improperly.
Question 4: A researcher needs to save a figure currently displayed on the screen to a high-resolution PNG file named 'figure1.png' for publication. Which command is most appropriate for this task?
- save('figure1.png')
- print(gcf, 'figure1.png', '-dpng', '-r300')
- exportgraphics(gcf, 'figure1.png', 'Resolution', 300) (Correct answer)
- savefig('figure1.png')
Correct answer: exportgraphics(gcf, 'figure1.png', 'Resolution', 300)
`exportgraphics` is the modern and recommended function for saving figures to various formats with control over properties like resolution. Using `exportgraphics(gcf, 'figure1.png', 'Resolution', 300)` saves the current figure (`gcf`) as a PNG with a resolution of 300 DPI. While `print` can also achieve this, `exportgraphics` offers a more streamlined syntax. `savefig` saves as a .fig file, and `save` is for workspace variables.
Question 5: A user wants to create a stacked bar chart from a numeric matrix `data`, where each column of `data` represents a category and each row represents a series. Which command correctly generates a stacked bar chart?
- bar(data, 'stacked') (Correct answer)
- plot(data, 'Type', 'stackedbar')
- stackedbar(data)
- bar(data, 'Style', 'stack')
Correct answer: bar(data, 'stacked')
To create a stacked bar chart, you use the `bar` function and provide the `'stacked'` option as an additional argument. This tells MATLAB to stack the values from each row within the same bar for each column. The other functions or options are not valid MATLAB syntax for creating a stacked bar chart.
Question 6: An engineer has an Excel file, 'test_results.xlsx', with data on multiple worksheets named 'Trial1', 'Trial2', and 'Trial3'. What is the first step required to programmatically import the data from 'Trial2' into a table?
- Use `xlsread('test_results.xlsx', 'Trial2')` as it's the only way.
- Open the file in Excel and save 'Trial2' as a separate CSV file.
- Use `readtable('test_results.xlsx', 'Sheet', 'Trial2')`. (Correct answer)
- First, use `sheetnames('test_results.xlsx')` to get all sheet names, then loop through them.
Correct answer: Use `readtable('test_results.xlsx', 'Sheet', 'Trial2')`.
The `readtable` function can directly access specific worksheets within an Excel file by using the 'Sheet' name-value pair argument. This allows for direct and efficient importing without needing to open Excel or pre-process the file. While getting all sheet names first is a valid strategy for importing all sheets, it's not the necessary first step for importing just one specific sheet. `xlsread` is no longer the recommended function.
A data analyst has a CSV file named 'sensor_data.csv' with mixed data types (numbers and text) and a header row.
Which function is most suitable for importing this data into MATLAB as a table, preserving the data types and using the header for variable names?