how to write code for real time graph in gui matlab
1 view (last 30 days)
Show older comments
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
Accepted Answer
Geoff Hayes
on 14 Mar 2015
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!
2 Comments
Geoff Hayes
on 14 Mar 2015
Shashank - you have to do more than just post some code and indicate that your project is due by a certain date. What difficulties are you facing? What is preventing you from moving forward? If you are observing a particular error then include the full error message in your question.
More Answers (0)
See Also
Categories
Find more on Specifying Target for Graphics Output 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!