Passing Data to Stan with MatlabStan

Hey everyone,

I have written and compiled my first Stan program and am working through the troubleshooting steps to get it up and running. The problem I’m running into is how to pass the data along from MATLAB. The data takes the form of a structural variable having a real value for the number of participants and a vector for the number of trials per participant, as well as a cell per participant with the matrix of their responses and a cell per participant of the stimuli they were presented. I will include the data block below. This is what the structural variable in Matlab looks like:

Alld.ns
Alld.nt{i}[1,ns]
All.resps{i}[4,ns]
All.oris{i}[1,ns]

The error I receive is:

Operator ‘>’ is not supported for operands of type ‘cell’.

Error in mstan.rdump (line 22)
if any(data{i}(:) > intmax(‘int32’))

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

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

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

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

data {

	int ns;													//number of subjects
	array[ns] int nt; 											//number of trials
	array[ns] matrix[4,nt[ns]] resps;									//responses
	array[ns] vector[nt[ns]] oris;										//orientations
	
}

Operating System: macOS Ventura 13.6
Interface Version: MatlabStan-2.15.1.0

So I’m just looking for advice on the best way to format the data to hand it from Matlab to MatlabStan (including edits to the way the data is declared in the .stan file if necessary)

Thanks!

Hello,

Unfortunately, MatlabStan isn’t actively maintained and doesn’t support the more recent data type introduced in Stan. I think the easiest for you would be to export the data manually and then use a Python* or R interface to stan.

*Python has some function to read directly Matlab mat file (scipy.io.loadmat — SciPy v1.11.2 Manual)

Thanks! There is a good chance we will be porting over to Python in the near future, though I am not familiar with the language yet myself. This might be as good an excuse as any to finally start learning it