Running dynare in a matlab function

I am trying to optimize welfare for a set of parameters in dynare.
I would like to run the dyname .m file with a function statement, which would have as output the Welfare index.
Apparently dynare does not execute within the function. Is there a trick to remedy this, to get dynare to run within a matlab function statement?
My procedure is to change a parameter, insert it into “load_initial_values” matrix in the dynare program, then execute the dynare .m file,
and then compute welfare. I have a series of optimization functions (simulated annealing and the genetic algorithm) which I would like to use.
Thanks, Paul McNelis

What do you mean by “function statement”? Do you mean function?

Why do you say that dynare won’t execute within a function?

Can you post your code?

Normally you should be able to run dynare within a MATLAB function. To retrieve results, just copy them back from the global oo_ structure.