Timer - plotting graph to axe in current figure

Hello, i have problem. I am passing parameters to timer Fcn like this:
%gui openingfcn (not the whole code) + nested timer function in it
myfigure = gcf;
guidata(myfigure,handles);
t = timer;
t.Period = 2;
t.ExecutionMode = 'fixedRate';
t.TimerFcn = @mytimer_cb;
t.BusyMode = 'drop';
t.UserData = myfigure;
start(t);
function mytimer_cb(h,~) % here starts timer nested function
hObject = h.UserData;
handles = guidata(hObject);
Then (lower in the timer Fcn) I want to plot graph in current figure like this:
axes(handles.axes_realbezmod);
h = plot( bezmod_x, bezmod_y, 'ob' );
But the results looks like this (the graph is not in figure):
I tried findall(0,'type','figure'), I tried to set a parent, but it didn't work.
Do you have any other ideas?
Thanks

 Accepted Answer

Adam
Adam on 7 Apr 2016
Edited: Adam on 7 Apr 2016
Try the more explicit version which is always better to use:
h = plot( handles.axes_realbezmod, bezmod_x, bezmod_y, 'ob' );
If that doesn't work it should at least fail in some way other than just creating a new figure which may help diagnose the problem.
Looking again at your code though you seem to be calling your time callback with no arguments passed in whereas the function expects what I assume if a figure handle.
I haven't really used timer for anything but I assume you need more like:
t.TimerFcn = @() mytimer_cb( h );
assuming h is the handle you expect within your timer callback.

3 Comments

It works, thanks for help.
Another "stupid" question. How I can add xlabel, ylabel and grid to this graph now? It also creates me a new figure. I can handle the legend, but not labels and grid. Could you also help me?
Thanks
Use the same technique. All these functions now accept an axes handle as their first argument in one of the function overloads so e.g.
xlabel( hAxes, 'SomeLabel' )
will explicitly apply the label to the given axes.
I used to uses the technique you use above until I kept running into unexpected (for me back then) issues with new axes getting created or things plotting to the wrong figure. This explicit method of telling it which axes to use avoids all that.
Thanks, everything is working properly.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!