for my project i need to search for the optimal simple monetary policy rule and calibrate some moments using volatilities.
My initial approach for both types of problems was to use a solver like fmincon, since both are problems of the type
max f(x) for x element X
where x is a vector of parameters and f(x) is a subset of the dynare output.

Yet it seems you can not call dynare and access the dynare output inside a matlab function , at least i didnt manage.
Is there a way this can be done? If not is there a generic optimization function included in matlab?

Here is a quick and easy example for anyone who wants to optimize over some output of dynare:
(In this example I optimize over a scalar between 1.1 and 10 using fmincon)

Furthermore save this matlab function in the current folder

global oo_ M_ options_ % get Dynare structures; used to pass them on to resol.m
%% set parameter for use in Dynare
set_param_value('phi1',xopt(1));
[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); %run model solution in Dynare
if info %solution was not successful
fval=10e6+sum([xopt(1),xopt(2),xopt(3),log(xopt(4)) ].^2); %return with penalty
else
% compute objective function
fval=oo_.steady_state(1); %this is an arbitary choice; here you can have e.g. some value function for optimal policy or some model moment for SMM
end

the above works fine for the OSR purpose (where all relevant statistics are in oo_.dr) if you want to do SMM you need to add some lines because resol doesnt update oo_ completely.
In particular the theoretical variance and mean are not recomputed.

Here is what you need to add to the above code after the resol comand to get the theoretical moments recomputed:

if isempty(options_.qz_criterium)
options_.qz_criterium = 1+1e-6;
end
options_.noprint=1;
disp_th_moments(oo_.dr, M_.endo_names(1:M_.orig_endo_nbr, :));

Note that you cant change the shock variances with set_param_value. Hence write
var uA = sigma_A^2; by var uA=1 ;
and
uA by uA*sigma_A

A more general version that also allows for IRF matching would be to use

if isempty(options_.qz_criterium)
options_.qz_criterium = 1+1e-6;
end
options_.noprint=1;
set_param_value('nu',nu);
% manually set the parameter that is looped over
info=stoch_simul(var_list_);
% get decision rules by directly calling Dynare routine directly instead of
% all of Dynare
if info~=0
error('The model did not correctly solve')
end