concatenate multiple 'OutputFcn' options in odeset?

Is there a way to output both odeplot+odephas2 after modifying the following?
options=odeset('OutputFcn',@odeplot,'OutputFcn',@odephas2);
[t,u]=ode23(@ode,[0;t],[u0;v0],options);

 Accepted Answer

Define a wrapper function to call whatever functions you want.
Note that the wrapper function itself will need to fulfill the requirements given under "Output Function" here:
Something like this:
function myfun
f1 = figure();
f2 = figure();
options = odeset('OutputFcn',@mywrapper);
[T,u] = ode23(@ode,[0;1],[0;1],options)
%%
function dudt = ode(t,u)
dudt=[u(2);-u(1)];
end
function status = mywrapper(varargin)
figure(f1);
s1 = odeplot(varargin{:});
figure(f2);
s2 = odephas2(varargin{:});
status = or(s1,s2);
end
end
Hopefully ODEPLOT et al are robustly written such that they can be called at the same time, i.e. have no assumptions on what the current figure/axes are. You will have to check that yourself.
EDIT: both of those plot functions call GCF, so I added the FIGURE() calls and tested it.

7 Comments

Thank you very much for the great code but I don't know how to put it into options=odeset('OutputFcn',@odeplot,'OutputFcn',@odephas2). Any other code I need to write in the main code?
options=odeset('OutputFcn',@mywrapper)
thanks but it doesn't work with this error below and the plot is neither odeplot nor odephas2
Unrecognized field name "anim".
"thanks but it doesn't work with this error below..."
Please show the complete error message. This means all of the red text.
MATLAB provides error messages to aid debugging and understanding the cause of the error. When you decide not to share this information it delays... debugging and understanding the cause of the error.
"...and the plot is neither odeplot nor odephas2"
Then I would not be very surprised if the function was not written fulfiling the requirements given in the documentation. So please show the function code or give a reference to where I can view this code.
And also show the wrapper function that you used.
function myfun
options=odeset('OutputFcn',@mywrapper);
[T,u]=ode23(@ode,[0;1],[0;1],options);
%%
function dudt=ode(t,u)
dudt=[u(2);-u(1)];
end
function status = mywrapper(varargin);
s1 = odeplot(varargin{:});
s2 = odephas2(varargin{:});
status = or(s1,s2);
end
end
Unfortunately both of those functions use GCF, which is the cause of that error. This is why I advised you to check for any assumptions they make about the current figure/axes.
The solution is to explicitly define the two figures ourselves, before calling the plotting functions, e.g.:
function myfun
f1 = figure();
f2 = figure();
options = odeset('OutputFcn',@mywrapper);
[T,u] = ode23(@ode,[0;1],[0;1],options)
%%
function dudt = ode(t,u)
dudt=[u(2);-u(1)];
end
function status = mywrapper(varargin)
figure(f1);
s1 = odeplot(varargin{:});
figure(f2);
s2 = odephas2(varargin{:});
status = or(s1,s2);
end
end
I modified my answer to include this change.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!