Matlab File I/O & Data Importing Quiz

Matlab
0 Passed
0% acceptance

40 comprehensive questions on MATLAB's file input/output operations, covering data import/export with readtable and writematrix, handling CSV and Excel files, managing .mat files, and navigating file systems — with 12 code examples to solidify your data handling skills.

40 Questions
~80 minutes
1

Question 1

When working with a mixed-type dataset containing both text and numeric columns in a CSV file, which MATLAB function is most appropriate to preserve the column headers and data types?

A
readtable, as it automatically detects headers and creates a table variable suitable for mixed data
B
readmatrix, because it is faster and handles text by converting it to NaN
C
load, because it is the universal command for all file types
D
fscanf, because it allows for precise character-by-character reading
2

Question 2

You have a large matrix of numeric data stored in an Excel file named 'data.xlsx' in the sheet 'RawData'. How would you import just this numeric data into a double-precision array?

matlab
% File: data.xlsx contains multiple sheets
% Sheet 'RawData' has numeric values

% Goal: Import only the numeric matrix
A
M = readmatrix('data.xlsx', 'Sheet', 'RawData');
B
M = load('data.xlsx', '-sheet', 'RawData');
C
M = xlsread('data.xlsx', 'RawData'); % (Legacy function)
D
M = importdata('data.xlsx');
3

Question 3

What is the primary advantage of using MATLAB's native .mat file format compared to CSV or text files?

A
It preserves MATLAB-specific data types (structs, cell arrays) and variable metadata efficiently
B
It is human-readable and can be opened in any text editor
C
It is the only format that supports numeric data
D
It automatically compresses data to zero size
4

Question 4

You need to save only the variables 'results' and 'config' from your current workspace to a file named 'experiment_final.mat'. Which command achieves this?

matlab
>> whos
  Name       Size            Bytes  Class
  data       1000x1000     8000000  double
  config     1x1               500  struct
  results    100x1             800  double
  temp_var   1x1                 8  double
A
save('experiment_final.mat', 'results', 'config')
B
save('experiment_final.mat', '-all')
C
save 'experiment_final.mat' -exclude data temp_var
D
writematrix([results, config], 'experiment_final.mat')
5

Question 5

When using the Import Tool in MATLAB, what is a key benefit of generating a script or function from your interactive selection?

A
It creates reproducible code that can be automated for similar files in the future
B
It modifies the original file to match MATLAB's format
C
It is the only way to import data into the workspace
D
It automatically deletes the source file after import
6

Question 6

How does the fullfile function contribute to writing robust MATLAB code?

matlab
folder = 'C:\Data';
file = 'experiment.csv';
path = fullfile(folder, file);
A
It constructs file paths using the correct separator for the operating system (e.g., backslash on Windows, forward slash on Linux)
B
It checks if the file exists and creates it if missing
C
It reads the full content of the file into memory
D
It converts relative paths to absolute paths
7

Question 7

You are processing a list of CSV files in a directory. Which command would you use to get a struct array containing information about all files matching the pattern '*.csv'?

A
dir('*.csv')
B
ls('*.csv')
C
what('*.csv')
D
exist('*.csv')
8

Question 8

What is the default behavior of the load command when used on a .mat file without assigning output variables?

matlab
>> load('mydata.mat')
A
It loads all variables from the file directly into the current workspace, potentially overwriting existing variables
B
It returns a structure containing the variables, without modifying the workspace
C
It displays the contents of the file but does not load them
D
It errors out if no output variable is provided
9

Question 9

You have a table T and want to export it to a CSV file named 'output.csv'. Which function is the most direct way to do this?

A
writetable(T, 'output.csv')
B
save('output.csv', 'T')
C
csvwrite('output.csv', T)
D
exportdata(T, 'output.csv')
10

Question 10

Consider the following code snippet. What will be the content of the variable 'data'?

matlab
% File 'config.txt' contains:
% 100
% 200
% 300

fID = fopen('config.txt', 'r');
data = fscanf(fID, '%d');
fclose(fID);
A
A column vector [100; 200; 300] containing the integers read from the file
B
A string "100 200 300"
C
A cell array {'100', '200', '300'}
D
The file ID handle
11

Question 11

Why might you choose to use readtable over readmatrix when importing a CSV file?

A
The file contains column headers and a mix of text and numbers that need to be preserved as distinct variables
B
The file is purely numeric and very large
C
You want to ignore all text data
D
readtable is the only function that can read CSV files
12

Question 12

You are writing a script that needs to read a file located two levels up from the current script's directory. Which command correctly constructs this path?

matlab
% Current location: /Project/Code/Scripts/
% Target file: /Project/Data/input.mat
A
fullfile('..', '..', 'Data', 'input.mat')
B
fullfile('//', '//', 'Data', 'input.mat')
C
path('up', 'up', 'Data', 'input.mat')
D
cd('..'); cd('..'); load('Data/input.mat')
13

Question 13

What happens if you try to use load on a file that is not in the current folder or the MATLAB search path?

A
MATLAB throws an error because it cannot locate the file
B
It searches the entire hard drive for the file
C
It prompts the user to browse for the file
D
It creates a new empty variable with that name
14

Question 14

You have a matrix A and want to append it to an existing text file 'log.txt' without overwriting the previous content. Which approach is valid?

matlab
A = [1 2 3];
% Goal: Append A to 'log.txt'
A
writematrix(A, 'log.txt', 'WriteMode', 'append')
B
writematrix(A, 'log.txt', 'Mode', 'add')
C
save('log.txt', 'A', '-append')
D
fopen('log.txt', 'w'); fprintf(A); fclose(fid);
15

Question 15

What is the purpose of the 'VariableNamingRule' parameter in readtable?

A
It controls how MATLAB handles column headers that are not valid MATLAB identifiers (e.g., containing spaces)
B
It renames the file before reading
C
It forces all variables to be named 'Var1', 'Var2', etc.
D
It determines the data type of the variables
16

Question 16

Which command would you use to verify if a specific file exists before attempting to read it?

matlab
filename = 'data.csv';
if ______
    data = readtable(filename);
else
    warning('File not found');
end
A
exist(filename, 'file') == 2
B
isfile(filename)
C
checkfile(filename)
D
dir(filename)
17

Question 17

When using writetable to export data to Excel, how can you specify the worksheet name?

A
Use the 'Sheet' name-value pair argument, e.g., writetable(T, 'file.xlsx', 'Sheet', 'Results')
B
Include the sheet name in the filename, e.g., 'file.xlsx:Results'
C
You cannot specify sheets; it always writes to the first sheet
D
Use the xlsopen command first
18

Question 18

You have imported a large dataset into a table 'T'. You want to save this table to a binary .mat file for faster loading next time. What is the command?

A
save('dataset.mat', 'T')
B
writetable(T, 'dataset.mat')
C
export(T, 'dataset.mat')
D
binarywrite(T)
19

Question 19

What does the 'DetectImportOptions' function do?

A
It analyzes a file and creates an object containing import settings (types, delimiters) that can be customized before reading
B
It automatically imports the data with the best guess settings
C
It detects if the file is a virus
D
It opens the Import Tool UI
20

Question 20

You are reading a text file where data fields are separated by semicolons instead of commas. How do you handle this with readtable?

matlab
% File: data.txt
% Name;Age;Score
% Alice;25;90
A
readtable('data.txt', 'Delimiter', ';')
B
readtable('data.txt', 'Separator', ';')
C
readtable('data.txt', 'Format', 'semi')
D
MATLAB automatically detects all delimiters perfectly every time
21

Question 21

Which function allows you to read a specific range of cells from an Excel file, such as 'B2:D10'?

A
readmatrix or readtable with the 'Range' parameter
B
load with the range argument
C
importdata with indices
D
xlsread only
22

Question 22

What is the result of using the 'TextType', 'string' option in readtable?

A
Text columns are imported as string arrays (double quotes) instead of cell arrays of character vectors
B
All data, including numbers, is converted to text
C
It forces the file to be read as a text file
D
It ignores numeric columns
23

Question 23

You need to create a new folder named 'Output' to store your results. Which command does this?

A
mkdir('Output')
B
newfolder('Output')
C
create('Output')
D
touch('Output')
24

Question 24

When using low-level file I/O with fopen, what does the permission 'w' signify?

matlab
fid = fopen('results.txt', 'w');
A
Open file for writing; discard existing contents (overwrite)
B
Open file for writing; append to existing contents
C
Open file for reading and writing
D
Open file for reading only
25

Question 25

How can you close a file handle 'fid' after you are done writing to it?

A
fclose(fid);
B
close(fid);
C
end(fid);
D
It closes automatically immediately
26

Question 26

You want to save a figure currently displayed in Figure 1 to a PNG image file. Which command is appropriate?

A
saveas(gcf, 'plot.png')
B
save('plot.png')
C
writetable(gcf, 'plot.png')
D
exportdata(gcf, 'plot.png')
27

Question 27

What is the function of the 'whos' command with the '-file' flag?

matlab
>> whos -file mydata.mat
A
It lists the variables inside the .mat file without loading them into memory
B
It loads the file and shows the workspace
C
It deletes the file
D
It checks if the file is corrupted
28

Question 28

Which command allows you to interactively select a file from the operating system's file dialog box?

A
uigetfile
B
inputfile
C
getfile
D
selectfile
29

Question 29

You have a cell array of strings 'C' and want to write it to a text file, line by line. Which low-level approach is correct?

matlab
C = {'Line 1', 'Line 2', 'Line 3'};
fid = fopen('output.txt', 'w');
for i = 1:length(C)
    ______
end
fclose(fid);
A
fprintf(fid, '%s\n', C{i});
B
write(fid, C{i});
C
disp(fid, C{i});
D
save(fid, C{i});
30

Question 30

What is a potential issue when using absolute file paths (e.g., 'C:\Users\Name\Project\data.csv') in your code?

A
The code will fail if run on a different computer or by a different user with a different directory structure
B
Absolute paths are slower to process
C
MATLAB cannot read absolute paths
D
It causes memory leaks
31

Question 31

How do you read a CSV file that has 5 header lines that need to be skipped?

A
readtable('file.csv', 'NumHeaderLines', 5)
B
readtable('file.csv', 'Skip', 5)
C
load('file.csv', 5)
D
It is impossible; you must manually delete the lines first
32

Question 32

What is the difference between 'save' and 'save -v7.3'?

A
-v7.3 uses HDF5 format, supporting variables larger than 2GB and better compression, unlike the default format
B
-v7.3 is an older, legacy format
C
There is no difference
D
-v7.3 saves only text data
33

Question 33

You want to load data from a file, but the filename is stored in a variable. Which syntax is correct?

matlab
fname = 'experiment_01.mat';
A
load(fname)
B
load fname
C
load 'fname'
D
import fname
34

Question 34

Which function would you use to read an image file (like .jpg or .png) into a numeric array?

A
imread
B
readimage
C
loadimage
D
importimg
35

Question 35

You have a structure 'S' and you want to save it to a .mat file such that the fields of S become individual variables in the file. How do you do this?

A
save('file.mat', '-struct', 'S')
B
save('file.mat', 'S')
C
save('file.mat', S.*)
D
writestruct(S, 'file.mat')
36

Question 36

What does the command 'type filename.m' do in the Command Window?

A
It displays the contents of the file as text in the Command Window
B
It shows the data type of the file
C
It executes the file
D
It opens the file in the Editor
37

Question 37

You are reading a file and want to ensure the file handle is closed even if an error occurs during reading. What structure should you use?

matlab
fid = fopen('data.txt');
try
    % Read data
    data = fread(fid);
catch
    % Handle error
end
______ % Ensure close
A
Use an onCleanup object or a finally block (if available in your version) to call fclose(fid)
B
Just put fclose(fid) at the end of the script
C
MATLAB never leaves files open on error
D
Use clear all
38

Question 38

Which command returns the current working directory?

A
pwd
B
cwd
C
dir
D
where
39

Question 39

You want to read a JSON file into a MATLAB structure. Which function should you use?

A
jsondecode(fileread('file.json'))
B
readjson('file.json')
C
loadjson('file.json')
D
import('file.json')
40

Question 40

When using 'readtable', how can you handle missing data in the file automatically?

A
Use the 'TreatAsMissing' parameter to specify placeholders and 'MissingRule' to define handling (e.g., fill, omit)
B
readtable cannot handle missing data
C
It always replaces missing data with zero
D
You must edit the file manually before import

QUIZZES IN Matlab