How do i create multiple plots in one diagram?
1 次查看(过去 30 天)
显示 更早的评论
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
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?
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!