Plotting two signals on one Graph using an array

5 visualizzazioni (ultimi 30 giorni)
John Smith
John Smith il 31 Mar 2016
Commentato: Star Strider il 31 Mar 2016
Hi all,
I am using the code from the website below to plot a signal.
I Modified the code as shown below so that I can display two signals on the same plot instead of just one signal. (Note that the comments with exclamation marks indicate the changes done). Why is it that only one signal is being displayed? How can I display both values of the array? Are there any other methods that I can use to achieve this? Any kind of help would be highly appreciated.
delete(instrfindall); % if any port is already opened by MATLAB its gonna find and close it
%User Defined Properties
serialPort = 'COM5'; % define COM port #
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Data'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = -1.5; % set y-min
max = 1.5; % set y-max
scrollWidth = 10; % display period in plot, plot entire data log if <= 0
%Define Function Variables
time = 0;
data = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,'-mo',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort)
disp('Close Plot to End Session');
fopen(s);
tic
while ishandle(plotGraph) %Loop when Plot is Active
dat = fscanf(s,'%f,%f'); % !!!!!!!!!!!!!!!!!!
if(~isempty(dat) && isfloat(dat))
count = count + 1;
time(count) = toc; %Extract Elapsed Time
data_array(count,:) = dat'; % !!!!!!!!!!!!!!!!!!
%Set Axis according to Scroll Width
set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data_array(time > time(count)-scrollWidth));
drawnow;
end
end
%Close Serial COM Port and Delete useless Variables
fclose(mbed); %close connection (this should never be reached when using while(1), but included for completeness)
delete (mbed) % deleting serial port object
clear mbed
clear all

Risposte (1)

Star Strider
Star Strider il 31 Mar 2016
I can’t run your code and I have difficulty following it.
See if the hold and drawnow functions (separately or together — you’ll have to experiment) will do what you want.
  8 Commenti
John Smith
John Smith il 31 Mar 2016
Another small question. I am plotting multiple graphs (something similar as the code below), should I include drawnow below each plot or just one at the end? Thanks.
set(plotGraph1,'XData',time(time > time(count)-scrollWidth),'YData',data1(time > time(count)-scrollWidth));
axis(figure1,[time(count)-scrollWidth time(count) min max]);
set(plotGraph2,'XData',time(time > time(count)-scrollWidth),'YData',data2(time > time(count)-scrollWidth));
axis(figure2,[time(count)-scrollWidth time(count) min max]);
set(plotGraph3,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
axis(figure3,[time(count)-scrollWidth time(count) min max]);
Star Strider
Star Strider il 31 Mar 2016
If they are different figures or different subplots, include drawnow after the plot call in each one. That’s how I would do it.
If you want a fixed axis (using the axis function), put that just after the plot call:
function(#) OR subplot(#,#,#)
plot( ..., ...)
axis([xmin xmax ymin ymax])
drawnow
... ANYTHING ELSE ...
This order of function calls has worked for me in the past when I’ve coded animated plots.
For example:
[X,Y] = meshgrid(linspace(-5, 5, 50));
fcn = @(x,y,k) k*x.^2 + y.^2;
v = [1:-0.05:-1; -1:0.05:1];
for k1 = 1:2
for k2 = v(k1,:)
surfc(X, Y, fcn(X,Y,k2))
axis([-5 5 -5 5 -30 50])
drawnow
pause(0.01)
end
end

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by