0
0
MATLABdata~5 mins

Reading text files (readtable, textscan) in MATLAB - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Reading text files (readtable, textscan)
O(n)
Understanding Time Complexity

When reading text files in MATLAB, it's important to know how the time to read grows as the file gets bigger.

We want to understand how the reading time changes when the file size increases.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

filename = 'data.txt';
fileID = fopen(filename, 'r');
C = textscan(fileID, '%s %f %d', 'Delimiter', ',');
fclose(fileID);

T = readtable(filename, 'Delimiter', ',');

This code reads a text file twice: once using textscan and once using readtable, both reading all lines.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Reading each line of the file and parsing its contents.
  • How many times: Once for each line in the file, so the number of lines (n) times.
How Execution Grows With Input

As the file grows, the program reads more lines, so the work grows roughly in direct proportion to the number of lines.

Input Size (n)Approx. Operations
10About 10 line reads and parses
100About 100 line reads and parses
1000About 1000 line reads and parses

Pattern observation: The time grows steadily as the file gets bigger, roughly doubling if the file size doubles.

Final Time Complexity

Time Complexity: O(n)

This means the time to read the file grows linearly with the number of lines in the file.

Common Mistake

[X] Wrong: "Reading a file takes the same time no matter how big it is."

[OK] Correct: The program reads each line one by one, so more lines mean more work and more time.

Interview Connect

Understanding how file reading time grows helps you write efficient programs and explain your code clearly in interviews.

Self-Check

"What if we read only the first 100 lines instead of the whole file? How would the time complexity change?"