In MATLAB exist( x, 'file' ) takes forever

后端 未结 4 1418
时光取名叫无心
时光取名叫无心 2021-02-19 12:46

I am using exist(x, \'file\') to check for the existence of a file on my machine. The execution of this command takes FOREVER (over 10 seconds per call!).

My matl

4条回答
  •  再見小時候
    2021-02-19 13:42

    1. exist is a built in Matlab function. It is designed to check existence of other types of objects (such as variables in Matlab) as well as files. Being a built in function, it's not a simple to see how it is coded. At least on Windows, when you call exist('filename','file') it seemingly only makes one API call to the operating system to check the file existence. So either the operating system is taking a long time, or there is some bloat in the exist function making it run slowly. See the solutions from the other posters for ideas on how to make the operating system return its result more quickly

    2. People sometimes complain that running exist('filename','file') in a loop makes the loop very slow, this is due the call taking perhaps milliseconds and looping over a few thousand times. The solution here is to replace

        if exist('filename','file')   
          % your code
    

    with the line

        if java.io.File('filename').exists
          % your code
    

提交回复
热议问题