Updat GUI plot with new data

Dear all,
I have created a GUI that needs to update once a second in such a way that we see a moving wave. My code is triggerd by a Timer:
ind1 =obj.plot_Timestamp(1,1); % left boundary plot
ind2 =obj.plot_Timestamp(1,2); % right boundary plot
axes(obj.handle_GUI_1); %get acces to my GUI for ploting
plot(obj.x,obj.y1,'--r',obj.x,obj.y2,'b'); %plot a calculated wave and a forecasted wave
title('Wave Elevation [m]');
xlabel('Time [s]');
xlim([ind1 ind2]); % my left and right boundary to be shown in my plot changing every sec.
ylim([-2 2]);
legend('RAO','HSC');
drawnow;
When I start the simulation, the plot is created in my GUI but instead of updating my GUI it starts ploting in a new figure after the first update ( so after 1 sec I get a new figure with my moving wave opened on top of my GUI).
I have tried several things but nothing seems to help.
Anyone an idea how to fix this?
Thx in advanced
stijn

 Accepted Answer

LineH = plot(1:10, rand(1, 10));
TimerH = timer('TimerFcn', {@myTimerCallback, LineH}, ...
'ExecutionMode', 'fixedRate', ...
'Period', 1.0);
start(TimerH);
function myTimerCallback(TimerH, EventData, LineH)
OldYData = get(LineH, 'YData');
NewYData = OldYData + rand(size(OldYData)) * 0.1;
drawnow;
I do not understand what the "obj" is in your code. This is thought as an example of how to use the handle of a line for updating. Of course you could provide the handle of the axes object instead.
Especially in a timer callback it is safer to specify the 'Parent' property in the plot command: Otherwise the user can confuse Matlab by clicking on another object.
plot(obj.x,obj.y1,'--r',obj.x,obj.y2,'b', 'Parent', AxesH);
If you update the line's XData and YData only, there is no need to create the the title, xlabel and legend again.

7 Comments

Or just
plot( AxesH, obj.x,obj.y1,'--r',obj.x,obj.y2,'b' )
Thank you for your response,
But unfortunately both option keep giving the same result:
- first plot is made in GUI
- every other pops up in new figure.
@Jan Simon: Im using objects so obj. stands for object.
Jan
Jan on 21 Mar 2017
Edited: Jan on 21 Mar 2017
@Stijn: Then you code does something else. As long as the parent has been specified, the diagram can appear at the wanted location only. If a new figure is created, this is triggered by a command, which does not match to the suggested solution. Please use the debugger to step through the code line by line until you find the command, which opens a new figure. Without seeing the code, I cannot guess more details.
I thought that "obj" means "object", but it is not clear how the data are provided to the timer's callback.
@Jan Simon: Thank you for your response. Strangely it does work perfectly when I use the debugger and walk through it step by step. Only when I let it run by itself it does not work and a new figure opens which refreshes every second.. Im now thinking about adding a 'hold' after creation and labeling the axces. Desperate for a solution :)
If you want to keep updating a plot on an axes you are far better off keeping the handle to the original plot and simply updating its XData and YData (and any other properties that may need updating, but generally just these for a line plot) and keep the same line handle throughout e.g. something like this is what I do regularly
if isempty( obj.hLine )
obj.hLine = plot( hAxes, xData, yData )
else
obj.hLine.XData = xData;
obj.hLine.YData = yData;
end
You may need to update the axes xlims and ylims yourself depending how you have that set, but it if you are in auto limits mode they should update anyway.
This method is a lot more efficient that constantly plotting and throwing away and plotting again, creating hundreds or thousands of new graphics objects with only ever one at a time. This way you create a single line object and you just update its properties as you would a normal class object.
Thank you! instead of redrawing the plot it is now refreshing the data! Below find my code:
if isempty( obj.hLine ) % Create plot first time: wave elevation
obj.hLine = plot(obj.handle_GUI_1,obj.timeH,obj.zeta,'--r',obj.timeH,obj.zetaH,'b'); %plot wave elevation
legend(obj.handle_GUI_1,'RAO','H');
title(obj.handle_GUI_1,'Wave Elevation [m]');
xlabel(obj.handle_GUI_1,'Time [s]');
else %refresh data and axes
obj.hLine.obj.timeH = obj.timeH;
obj.hLine.obj.zeta = obj.zeta;
obj.hLine.obj.timeH = obj.timeH;
obj.hLine.obj.zetaH = obj.zetaH;
xlim(obj.handle_GUI_1,[ind1 ind2]);
ylim(obj.handle_GUI_1,[-2 2]);
drawnow;
end
Question B:
Now i got the plot moving, but so are my axis. I would like to keep my axis fixed, independent from the plot. That is, I would like my axis to tell me the relative distance to a certain point from 'Now' = 0. Therefore i would like my axis to state for instance: [ 0 30 60 90 120 ]. I am trying as follows but not succeeding:
set(gca,'xticklabel',{'Now','100','200','300','400'})
Anyone an idea how to fix this?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!