MATLAB: How to write code for real time graph in gui matlab

doit4mematlab guirealtime graph

x = 0:pi/100:10*pi;
y = sin(x);
z = cos(x);
w = x./x;
% loop
n = numel(x);
% axes1 plot
h(1) = plot(handles.axes1, x(1), y(1));
h(2) = plot(handles.axes1, x(1), z(1));
% axes2 plot
h(3) = plot(handles.axes2, x(1), w(1));
for i = 1:n-1
% axes1
set(h(1), 'XData', x(1:i), 'YData', y(1:i));
set(h(2), 'XData', x(1:i), 'YData', z(1:i), 'color', 'green');
% axes2
set(h(3), 'XData', x(1:i), 'YData', w(1:i), 'color', 'red');
drawnow;
pause(1/10);
end
hold off

Best Answer

Shashank - running your above code (in a GUI with two axes) generates the following error
Error using handle.handle/set
Invalid or deleted object.
Error in untitled>untitled_OpeningFcn (line 77)
set(h(1), 'XData', x(1:i), 'YData', y(1:i));
as the handle to the plot graphics object is no longer valid. When creating multiple plots on the same axes, you need to invoke hold so that the current plot, h(1), is retained when the second plot, h(2), is added.
Add the following two lines (though only the first is needed since it is axes1 that has two different plots added to it)
hold(handles.axes1,'on');
hold(handles.axes2,'on');
h(1) = plot(handles.axes1, x(1), y(1));
h(2) = plot(handles.axes1, x(1), z(1));
h(3) = plot(handles.axes2, x(1), w(1));
% etc.
Try the above and see what happens!