Run with params from MATLAB

I want to run a Dynare script from a MATLAB function, with flexible parameters. My ludicrously hacky way to do it so far is along the lines of:

function irf_dat = get_irf_data(alpha, beta)
    filename = 'RBC_IRF_matching_results.mat'
    delete filename

    dynare RBC_IRF_matching     %Add alpha and beta param values here, somehow
    irf_dat_temp = load(filename).oo_.irfs.ghat_eps_g
    irf_dat = mean(irf_dat_temp)

(and this doesn’t even use alpha and beta!)

(a) How can I pass parameters to the Dynare script?
(b) How can I overhaul this monstrosity, so it doesn’t have to actually write/read from data files?

The reason for all this: I want to be able to differentiate certain model outputs with respect to parameters (and compute a numerical Jacobian of the outputs)

Thanks! (and sorry if I’ve burnt your eyes out due to such hacky/awful code)

@jpfeifer any ideas…? Help hugely appreciated if you have a moment

The inner part of your loop is essentially contained in