Matlabstan on windows - cannot open file to write

I am trying to get matlabstan running on Windows 10. I have cmdstan installed and running as expected. It compiled and I can run the sample code with out any issues.

I have tried this with cmdstan 2.23.0 and 2.17.1. I have tried MATLAB R2020a,R2018a and R2013a. I have the same issue in all cases.

When I run the eight_schools or rats sample code I get the following:

fit = stan(‘model_code’,schools_code,‘data’,schools_dat);
Error using mstan.write_lines (line 9)
Cannot open file to write

Error in StanModel/update_model (line 1197)
mstan.write_lines(fname,arg);

Error in StanModel/update_model (line 1165)
self.update_model(‘write’,arg);

Error in StanModel/set.model_code (line 403)
self.update_model(‘model_code’,model);

Error in StanModel/set (line 258)
self.model_code = p.Results.model_code;

Error in StanModel/sampling (line 772)
self.set(varargin{:});

Error in stan (line 146)
fit = model.sampling(p.Unmatched);

If I use ‘file’ instead of ‘model_code’ I get:>> fit = stan(‘file’,‘eight_schools.stan’,‘data’,schools_dat);
Error using StanModel/set.file (line 316)
File does not exist

Error in StanModel/set (line 262)
self.file = p.Results.file;

Error in StanModel/sampling (line 772)
self.set(varargin{:});

Error in stan (line 146)
fit = model.sampling(p.Unmatched);

eight_schools.stan is in my matlab path. If I use the full path to the file I get:

fit = stan(‘file’,‘c:\rtn\MatlabStan\eight_schools.stan’,‘data’,schools_dat);
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in mstan.rdump>@()fclose(fid) (line 22)
c = onCleanup(@()fclose(fid));

Error in onCleanup/delete (line 80)
obj.task();

Error in mstan.rdump (line 9)
if isstruct(content)

Error in StanModel/set.data (line 743)
mstan.rdump(fname,d);

Error in StanModel/set (line 282)
self.data = p.Results.data;

Error in StanModel/sampling (line 772)
self.set(varargin{:});

Error in stan (line 146)
fit = model.sampling(p.Unmatched);

In mstan.rdump (line 9)
In StanModel.set.data (line 743)
In StanModel/set (line 282)
In StanModel/sampling (line 772)
In stan (line 146)
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in mstan.rdump (line 35)
fprintf(fid,‘%s ← %s\n’,vars{i},num_list);

Error in StanModel/set.data (line 743)
mstan.rdump(fname,d);

Error in StanModel/set (line 282)
self.data = p.Results.data;

Error in StanModel/sampling (line 772)
self.set(varargin{:});

Error in stan (line 146)
fit = model.sampling(p.Unmatched);

I have set stan_home.m to the parent directory of my cmdstan install. The tests for processmanager and cmdstan work. ProcessManager, processstate and MatlabStan are all in my path.

Has any one seen this before?

Thank you.

1 Like

I fear you are on your own as MatlabStan is not actively maintained, we’re hoping someone would pick it up one of those days. However from the error message, I would guess there is a problem writing temporary file with the Stan code. This might be some issue around permissions to write to temporary folder and/or the location of the temporary folder. Other than that I can’t really help you.

Best of luck!