필터 지우기
필터 지우기

How do i create multiple plots in one diagram?

조회 수: 2 (최근 30일)
Julozert
Julozert 2018년 12월 7일
답변: Lucademicus 2018년 12월 12일
Hello guy im working currently with a robot and i need to visualize the measurements of the robot as a live plot.
I got a matlab script but i just creates one plot.
Here is my script:
function[out]=test_liveplot()
p.dev= '/dev/ttyUSB0';
p.baudrate = 57600;
p.terminator = 10;
s = serial(p.dev, ...
'BaudRate', p.baudrate, ...
'StopBits', 2, ...
'Terminator', p.terminator, ...
'TimeOut',5);
fopen_extended(s);
figure('Name','Live-Plot'); hold on; grid on;
ylabel('velocity [tics/100ms]'); xlabel('time/s');
hl=plot(nan,'r-');
hr=plot(nan,'b-');
out=zeros(0,4);
active=true;
while active
data=fscanf(s, ...
'%d %d %d %d');
if isempty(data)
active=false;
continue;
end
out(end+1,:)=data';
set(hl,'XData', double(out(:,1) )/1000, ...
'YData', out(:,4));
set(hr,'XData', double(out(:,1))/1000, ...
'YData', out(:,4));
drawnow
end
disp('No data left, loop ended.');
fclose(s);
function fopen_extended(s)
try
fopen(s);
catch err
if(strcmp(err.identifier,'MATLAB:serial:fopen:opfailed'))
try
ports=instrfind('Port', s.Port);
fclose(ports);
fopen(s);
catch err
disp('couldnt open connection');
disp('no rights')
rethrow(err);
end
else
rethrow(err);
end
end
the robots sends all 4 values to matlab but matlab only creates the plot of one of those values but i need all of them in the live plot.
what do i have to change to get it working?
  댓글 수: 2
Lucademicus
Lucademicus 2018년 12월 7일
Do I understand it correctly that of the 4 values, one should represent the x-axis and the remaining 3 are values of the y-axis, hence you want 3 plots?
Julozert
Julozert 2018년 12월 7일
@Lucademicus
yes exactly. I want the time as x-axis and the speed value of the right and left motor as y-value

댓글을 달려면 로그인하십시오.

답변 (1개)

Lucademicus
Lucademicus 2018년 12월 12일
This parts reads the data, the 4 %d's suggests it are 4 doubles which are read:
data=fscanf(s, ...
'%d %d %d %d');
This part sets the dynamic data sources of the X- and the Y-axis:
set(hl,'XData', double(out(:,1) )/1000, ...
'YData', out(:,4));
set(hr,'XData', double(out(:,1))/1000, ...
'YData', out(:,4));
I'm not sure if 'hl' should be the 'left motor', and 'hr' should be the 'right motor', but the 'YData' is both out(:,4) so these plots are equal.
I am assuming out(:,1) is the x-data, out(:,2), out(:,3) and out(:,4) are pointing to the relevant y-data (your motor speeds):
function[out]=test_liveplot()
p.dev= '/dev/ttyUSB0';
p.baudrate = 57600;
p.terminator = 10;
s = serial(p.dev, ...
'BaudRate', p.baudrate, ...
'StopBits', 2, ...
'Terminator', p.terminator, ...
'TimeOut',5);
fopen_extended(s);
figure('Name','Live-Plot'); hold on; grid on;
ylabel('velocity [tics/100ms]'); xlabel('time/s');
plotA=plot(nan,'r-');
plotB=plot(nan,'b-');
plotC=plot(nan,'g-');
out=zeros(0,4);
active=true;
while active
data=fscanf(s, ...
'%d %d %d %d');
if isempty(data)
active=false;
continue;
end
out(end+1,:)=data';
set(plotA,'XData', double(out(:,1) )/1000, ...
'YData', out(:,2)); % plotting the 2nd double of the 'out'-matrix
set(plotB,'XData', double(out(:,1))/1000, ...
'YData', out(:,3)); % plotting the 3rd double of the 'out'-matrix
set(plotB,'XData', double(out(:,1))/1000, ...
'YData', out(:,4)); % plotting the 4th double of the 'out'-matrix
drawnow
end
disp('No data left, loop ended.');
fclose(s);
function fopen_extended(s)
try
fopen(s);
catch err
if(strcmp(err.identifier,'MATLAB:serial:fopen:opfailed'))
try
ports=instrfind('Port', s.Port);
fclose(ports);
fopen(s);
catch err
disp('couldnt open connection');
disp('no rights')
rethrow(err);
end
else
rethrow(err);
end
end

카테고리

Help CenterFile Exchange에서 External Language Interfaces에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by