Plotting two signals on one Graph using an array

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

Answers (1)

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 Comments

@Srar Strider . Thanks for the reply. I tried both but neither is working. I now did my changes and the plots will be set on different figures. Now I have another small problem. How can I remove the values form the x-axis? From the forum I found that one should use 'XTick', [] , but were exactly should I place it please? I tried it as shown below but it did not work:
set(plotGraph,'XData',time(time > time(count)-scrollWidth),'XTick', [],'YData',data_array(time > time(count)-scrollWidth));
That looks correct, and it worked for me when I used it with a sample plot. I can’t determine what the problem may be.
Am am getting the following error:
Error using set
Invalid property found.
Object Name: line
Property Name: 'XTick'.
Can it be that it is conflicting with the graph declarations at the beginning of the code?
You need to address the current axis handle, not the line handle, with that:
set(gca, 'XTick',[])
That should work.
It worked, Thanks :)
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]);
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

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 31 Mar 2016

Commented:

on 31 Mar 2016

Community Treasure Hunt

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

Start Hunting!