How do you handle resources in MATLAB in an exception safe manner? (like “try … finally”)

后端 未结 2 1720
无人及你
无人及你 2020-11-29 02:08

Since there is no finally clause to the try-catch block in MATLAB, I find myself writing lots of code like the following:

fid = fopen(filename);
if fid==-1
          


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 02:37

    My preference is to create a FileHandle class with a delete method that closes the file when the object goes out of scope. Also gives you the opportunity to do other more natural file handle-y things.

提交回复
热议问题