Updat GUI plot with new data

4 views (last 30 days)
Stijn Hillenius
Stijn Hillenius on 21 Mar 2017
Commented: Stijn Hillenius on 6 Apr 2017
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

Jan
Jan on 21 Mar 2017
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
Stijn Hillenius
Stijn Hillenius on 28 Mar 2017
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
Stijn Hillenius
Stijn Hillenius on 6 Apr 2017
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!