Sometimes it runs, sometimes it doesn't

Dear everybody,

I am a beginner and struggeling at the moment with the following problem:
Sometimes my code runs and sometimes it doesn’t, sometimes it gets only through the half of the iterations required before it crashes.
At all times I get the same answer:

Processing outputs …ERROR: Can’t open file Paper_bank_run_DSGE_set_auxiliary_variables.m for writing

Error using dynare (line 174)
DYNARE: preprocessing failed

Error in Paper_bank_run (line 189)
dynare Paper_bank_run_DSGE noclearall

The problem lies within the second half (unanticipated run) of the matlab file, the first one works just fine (at least it seems that way). The second one calls the paper_bank_run_DSGE.mod file as part of its iteration process and that’s where the ‘fun’ begins.

I would appreciate any suggestions! Thanks a lot in advance.
Paper_basic_DSGE.mod (3.11 KB)
Paper_bank_run_DSGE.mod (3.22 KB)
Paper_bank_run.m (8.68 KB)

I am not sure what you are actually iterating over, but if you call Dynare repeatedly, the preprocessor may be faster than your harddisk so that it tries to create files that your system is still deleting. If you cannot build your loop without running the preprocessor several times, it might be necessary to give the harddisk more time by adding a

command from Matlab. Try one second first.

Thank you very much Johannes, it did the trick! As usual, the solution is pretty straightforward now that I know it^^

Just a short explanation to the file: It’s about to become a model of a sunspot equilibrium, a ‘random state’ in which the bank is thrown and that triggers a bank run. Therefore I need to ‘construct’ a new path backwards from the steady state to the bank run (which happens after a shock). But there are still a few bugs, of course. But thanks to you I am on them now - previously I just couldn’t see what I got^^