Does matlab automatically close a file after loading it
25 views (last 30 days)
Show older comments
Matlab_Student
on 25 Jan 2018
Answered: Walter Roberson
on 25 Jan 2018
I have a big for loop that loads each file in a directory and performs some calculations. I wonder if matlab still keeps these file loaded after it finishes the for loop.
0 Comments
Accepted Answer
Walter Roberson
on 25 Jan 2018
load() and imread() and audioread() and csvread() and dlmread() automatically close files they open.
On Mac and Linux, and on MS Windows that does not have Excel installed, xlsread() and readtable() automatically close files they open.
On MS Windows with Excel installed, xlsread() and readtable() use ActiveX to talk to Excel to do the loading. In the last few releases (starting R2016a or so), when it does that, it "caches" the previous connection, so that if you read from the same file several times, it does not have to rebuild the connection. My memory is that only the immediately previous file is retained, so if you were to read P, P, Q, P then the second P would reuse the connection, the Q after that would close the connection and build a new one for Q, and the P after that would close that connection and open a new on for P again.
Historically, there have been cases where some operations did not always close files when they should have.
At any point, you can determine how many files are open by looking at the output of fopen('all')
0 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!