Matlab File I/O & Data Importing Quiz
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.
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?
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?
% File: data.xlsx contains multiple sheets
% Sheet 'RawData' has numeric values
% Goal: Import only the numeric matrixQuestion 3
What is the primary advantage of using MATLAB's native .mat file format compared to CSV or text files?
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?
>> whos
Name Size Bytes Class
data 1000x1000 8000000 double
config 1x1 500 struct
results 100x1 800 double
temp_var 1x1 8 doubleQuestion 5
When using the Import Tool in MATLAB, what is a key benefit of generating a script or function from your interactive selection?
Question 6
How does the fullfile function contribute to writing robust MATLAB code?
folder = 'C:\Data';
file = 'experiment.csv';
path = fullfile(folder, file);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'?
Question 8
What is the default behavior of the load command when used on a .mat file without assigning output variables?
>> load('mydata.mat')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?
Question 10
Consider the following code snippet. What will be the content of the variable 'data'?
% File 'config.txt' contains:
% 100
% 200
% 300
fID = fopen('config.txt', 'r');
data = fscanf(fID, '%d');
fclose(fID);Question 11
Why might you choose to use readtable over readmatrix when importing a CSV file?
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?
% Current location: /Project/Code/Scripts/
% Target file: /Project/Data/input.matQuestion 13
What happens if you try to use load on a file that is not in the current folder or the MATLAB search path?
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?
A = [1 2 3];
% Goal: Append A to 'log.txt'Question 15
What is the purpose of the 'VariableNamingRule' parameter in readtable?
Question 16
Which command would you use to verify if a specific file exists before attempting to read it?
filename = 'data.csv';
if ______
data = readtable(filename);
else
warning('File not found');
endQuestion 17
When using writetable to export data to Excel, how can you specify the worksheet name?
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?
Question 19
What does the 'DetectImportOptions' function do?
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?
% File: data.txt
% Name;Age;Score
% Alice;25;90Question 21
Which function allows you to read a specific range of cells from an Excel file, such as 'B2:D10'?
Question 22
What is the result of using the 'TextType', 'string' option in readtable?
Question 23
You need to create a new folder named 'Output' to store your results. Which command does this?
Question 24
When using low-level file I/O with fopen, what does the permission 'w' signify?
fid = fopen('results.txt', 'w');Question 25
How can you close a file handle 'fid' after you are done writing to it?
Question 26
You want to save a figure currently displayed in Figure 1 to a PNG image file. Which command is appropriate?
Question 27
What is the function of the 'whos' command with the '-file' flag?
>> whos -file mydata.matQuestion 28
Which command allows you to interactively select a file from the operating system's file dialog box?
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?
C = {'Line 1', 'Line 2', 'Line 3'};
fid = fopen('output.txt', 'w');
for i = 1:length(C)
______
end
fclose(fid);Question 30
What is a potential issue when using absolute file paths (e.g., 'C:\Users\Name\Project\data.csv') in your code?
Question 31
How do you read a CSV file that has 5 header lines that need to be skipped?
Question 32
What is the difference between 'save' and 'save -v7.3'?
Question 33
You want to load data from a file, but the filename is stored in a variable. Which syntax is correct?
fname = 'experiment_01.mat';Question 34
Which function would you use to read an image file (like .jpg or .png) into a numeric array?
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?
Question 36
What does the command 'type filename.m' do in the Command Window?
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?
fid = fopen('data.txt');
try
% Read data
data = fread(fid);
catch
% Handle error
end
______ % Ensure closeQuestion 38
Which command returns the current working directory?
Question 39
You want to read a JSON file into a MATLAB structure. Which function should you use?
Question 40
When using 'readtable', how can you handle missing data in the file automatically?
