Determine available disk space

8 views (last 30 days)
Mark Hayworth
Mark Hayworth on 23 Jan 2023
Edited: dpb on 23 Jan 2023
I was acquiring a bunch of images and got a write error from imwrite that it couldn't write the file. It turned out the problem was that the drive was full. I know how many images I need to save and I would like to determine, in advance, if there is enough space to capture all the images that I need.
Is there a MATLAB function to determine the amount of available drive space? A bonus would be the amount of disk space used so far, and the total capacity of the drive.
I tried
[status, cmdout] = system('dir c:')
0 File(s) 0 bytes
11 Dir(s) 46,905,581,568 bytes free
but then it involves some additional tricky parsing of cmdout string, plus it takes a few seconds. I was wondering if there was a more direct way to get the available disk space as a number. Like a built-in function or something. Searches of the documentation and Answers forum turned up nothing.

Answers (3)

Image Analyst
Image Analyst on 23 Jan 2023
I didn't find any MATLAB functions but a java method works:
>> free ="D:").getFreeSpace()
free =
  1 Comment
dpb on 23 Jan 2023
Much easier/simpler than shelling out, indeed, IA!
I "know nuthink!" to speak of of Java so don't ever know where/how to go look; I didn't find a convenient wrapper to the Win API already written so reverted to PS...

Sign in to comment.

dpb on 23 Jan 2023
Try this for comparison if on Windows...
!powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
on local drive (can't run on unix server) returns
>> !powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
dpb on 23 Jan 2023
PS. I've yet to figure out that there's a way to substitute PS for CMD with system -- TMW apparently doesn't believe there are any other shells. Same problem with Win10 using the JPSoft CMD replacement shell -- I forget where it broke; with earlier releases when back under NT4 and subsequent, MATLAB used to honor the command interpreter set with the system startup; I've been unable to make that work with recent releases although I've not made any attempts for quite_a_long_time_now™, just having become resigned to it although it is a pain to not have all the extra features available via system sometimes.

Sign in to comment.

Walter Roberson
Walter Roberson on 23 Jan 2023
Edited: Image Analyst on 23 Jan 2023
Image Analyst
Image Analyst on 23 Jan 2023
I tried that first. It also gave the "Unable to resolve the name" error.

Sign in to comment.


Find more on Startup and Shutdown in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!