Plotting real time serial data in two different axes goes to the wrong axes using one push button in MATLAB GUIDE

1 vue (au cours des 30 derniers jours)
Hello good people,
I am stuck with a problem in matlab gui. I am trying to plot 4 different outputs from arduino to matlab gui 2 axes (axes1 & axes2) using guide. The x axis is same for these two axes while ydata is changing according to the arduino output variables. I can plot the variables in two axes but they are not plotting according to my showed path. For example, i want to plot distance1,distance2 & distance3 to plot in axes1 and capa1[0] & capa1[1] to axes2 using one push button. What are the mistakes in Matlab code? Someone please correct me.
Thanks in Advance.
%%%%%% Arduino serial write code: (I am sending value from 0-255)
Serial.write(distance1);
Serial.write(distance2);
Serial.write(distance3);
Serial.write(capa1[0]);
Serial.write(capa1[1]);
%%%%%% Matlab Gui code:
function START_Callback(hObject, eventdata, handles)
% hObject handle to START (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
delete(instrfindall);
% Init and open the serial port
s = serial('COM3', 'baudrate', 115200);
fopen(s);
points = 100; %number of points on the graph at all times
data_period = 50; %data period in milliseconds
%x will be the time axis. The time between points is defined by %"data_period"
x = linspace(-points*data_period, points*data_period, points);
%y will hold the distance, for now all values will be 0 and will have the
%size defined by "points"
y = linspace(0,0,points);
z = linspace(0,0,points);
z1 = linspace(0,0,points);
c1 = linspace(0,0,points);
c2 = linspace(0,0,points);
axes(handles.axes1); % Define axes for plot
hold(handles.axes1,'on');
lh1=plot(handles.axes1,x,y);
lh2=plot(handles.axes1,x,z);
lh3=plot(handles.axes1,x,z1);
grid(handles.axes1, 'on');
axes(handles.axes2); % Define axes for plot
hold(handles.axes2,'on');
rh1=plot(handles.axes2,x,c1);
rh2=plot(handles.axes2,x,c2);
grid(handles.axes2, 'on');
% % set the x and y axis limits to [-points*data_period, points*data_period] and [0, 255] respectively
axis(handles.axes1,[-points*data_period, points*data_period, 0, 255]);
axis(handles.axes2,[-points*data_period, points*data_period, 0, 255]);
shg; %brings the figure to the front of all other windows
%
key = get(gcf,'CurrentKey'); %get the key currently pressed
while ( strcmp(key, 's') == 0) %this while will stop if you press the "s" key
key = get(gcf,'CurrentKey'); %get the key currently pressed
%
% %block until there's at least 1 byte available to read
while s.BytesAvailable == 0
end
%
% %push the all the values to the left of the graph
for k = 1:1:points-1
y(k) = y(k+1);
z(k) = z(k+1);
z1(k) = z1(k+1);
c1(k) = c1(k+1);
c2(k) = c2(k+1);
end
% %read and place value in the right of the graph
y(points) = fread(s,1); %Left graph
z(points) = fread(s,1); %Left graph
z1(points) = fread(s,1); %Left graph
c1(points) = fread(s,1); %Right graph
c2(points) = fread(s,1); %Right graph
% %save the value in distance without a ";" so we can read the number in
% %console
distance = y(points)
distance1 = z(points)
distance2 = z1(points)
raw_Capacitance1 = c1(points)
raw_Capacitance2 = c2(points)
% % edit just the data for the y axis on the graph. This is much, much
% % faster than ploting everything over and over again
set(lh1, 'YData',y);
set(lh2, 'YData',z);
set(lh3, 'YData',z1);
set(rh1, 'YData',c1);
set(rh2, 'YData',c2);
% %request the plot to be updated
drawnow;
%
end
%
close ALL %close all open figures
%
fclose(s); %close serial port
delete(s); %remove serial port from memory

Réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by