how to write code for real time graph in gui matlab

1 view (last 30 days)
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
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
raki
raki on 14 Mar 2015
Edited: Geoff Hayes on 14 Mar 2015
please help me for this sir in gui matlab i created 2 axis for pressure, temperatur and two push button for plot & stop, 3 push button for status indication , if the pressure value > 270 < 290 it will indicate narmal operation,if > 260 < 300 it will indicate safety limits and >300 < 260 it will system failure and i have to interface ardiuno. i written for pressure & i have to submit it on 18-march in my college,please help me..
serialPort = 'COM1';
serialObject = serial(serialPort);
fopen(serialObject);
%%Set the instrument in remote mode
fprintf(serialObject,'SYSTEM:REMOTE');
%%Set up the figure window
time = now;
voltage = 0;
figureHandle = figure('NumberTitle','off',...
'Name','pressure Characteristics',...
'Color',[0 0 0],'Visible','off');
% Set axes
axesHandle = axes('Parent',figureHandle,...
'YGrid','on',...
'YColor',[0.9725 0.9725 0.9725],...
'XGrid','on',...
'XColor',[0.9725 0.9725 0.9725],...
'Color',[0 0 0]);
hold on;
if (pressure > 270 && pressure < 290)
plotHandle = plot(axesHandle,time,voltage,'Marker','.','LineWidth',1,'g');
else if(pressure > 260 && pressure < 300)
plotHandle = plot(axesHandle,time,voltage,'Marker','.','LineWidth',1,'y');
else if(pressure < 250 && pressure > 300)
plotHandle = plot(axesHandle,time,voltage,'Marker','.','LineWidth',1,'r');
end
xlim(axesHandle,[min(time) max(time+0.001)]);
% Create xlabel
xlabel('Time','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create ylabel
ylabel('pressure in bar','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create title
title('pressure Characteristics','FontSize',15,'Color',[1 1 0]);
%%Set the time span and interval for data collection
stopTime = '10/07 21:53';
timeInterval = 0.005;
%%Collect data
count = 1;
while ~isequal(datestr(now,'mm/DD HH:MM'),stopTime)
time(count) = datenum(clock);
fprintf(serialObject,'MEASURE:pressure:DC?'); % To measure current the command is MEASURE:CURRENT:DC?
pressure(count) = fscanf(serialObject,'%f'); %#ok<SAGROW>
set(plotHandle,'YData',pressure,'XData',time);
set(figureHandle,'Visible','on');
datetick('x','mm/DD HH:MM');
pause(timeInterval);
count = count +1;
end
%%Put the instrument in local mode
fprintf(serialObject,'SYSTEM:LOCAL');
%%Clean up the serial object
fclose(serialObject);
delete(serialObject);
clear serialObject;
Geoff Hayes
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.

Sign in to comment.

More Answers (0)

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!