# Plot 3 graphs on same figure

1 view (last 30 days)
monkey_matlab on 1 Sep 2015
Answered: Walter Roberson on 1 Sep 2015
Hello,
From the code below, I wanted to get 3 plots on the same figure:
1. Plot 1 will run the x time-axis from 1 to 10 vs the next 3 columns on the y-axis
2. Plot 2 will run the x time-axis from 80 to 89 vs the next 3 columns on the y-axis
3. Plot 3 will run the x time-axis from 100 to 109 vs the next 3 columns on the y-axis
clear;
clc;
[FileName,PathName] = uigetfile('*.txt','Select data file');
fid = fopen( strcat(PathName,FileName) ,'rt' );
% Read the file and store into matrix v
i = 0;
v = [0 0];
while feof(fid) == 0
buffer = fscanf(fid, '%f', 4);
buffer = buffer';
if i == 0;
v = buffer;
else
v = vertcat(v,buffer);
end
i = i + 1;
end
% Time vector
time = v(:,1);
% Number vector
number_1 = v(:,2);
number_2 = v(:,3);
number_3 = v(:,4);
%hold on
plot(time,number_1,'-ro', time,number_2,'-.b', time, number_3); grid on; axis([1 120 -67 -45]);
xlabel('Time (secs)'); ylabel('Current (mA)'); legend('number1','number2','number3')
curtick = get(gca, 'XTick');
set(gca, 'XTickLabel', cellstr(num2str(curtick(:))));
Can you help me to modify the code to get my desired output?
Thanks.

Walter Roberson on 1 Sep 2015
numtime = size(v,1);
time1 = linspace(1, 10, numtime);
time2 = linspace(80, 89, numtime);
time3 = linspace(100, 109, numtime);
plot(time1,number_1,'-ro', time2,number_2,'-.b', time3, number_3);
grid on; axis([1 120 -67 -45]);
As you know the range of times you want but the number of rows of v you get is not constant, you need to interpolate the times to fit the number of rows you have.
... or was the idea that you want to extract from v the rows those times match those ranges, showing only a subset of the available data? If so then:
idx1 = 1 <= time & time <= 10;
idx2 = 80 <= time & time <= 89;
idx3 = 100 <= time & time <= 109;
v1 = v(idx1,:);
v2 = v(idx2,:);
v3 = v(idx3,:);
plot(v1(:,1), v1(:,2:4),'-ro', v2(:,1),v2(:,2:4),'-.b', v3(:,1),v3(:,2:4));
grid on; axis([1 120 -67 -45]);

### Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!