MATLAB: Plot two graphs in the same figure, that have different number of points from text file

MATLABplot

Hello,
I am trying to plot 2 graphs in the same figure, but I am having a problem as the two datasets contain different number of points. How can I plot the graphs in the same figure but in different plots. That is, plot a will have (col 1, col2), plot 2 will have (col3, col4).
Here is what I have so far:
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
% Frequency vector
freq1 = v(:,1);
freq2 = v(:,3);
% Phase noise vector
spectrum1 = v(:,2);
spectrum2 = v(:,4);
subplot(1, 2, 1);
plot(freq1,spectrum1);
ax.XTick = [380.0145:380.0345:.1];
grid on; axis([380.0145 380.0345 -130 -10]);
title('Silent Carrier (No Modulation)');
xlabel('Frequency (MHz)'); ylabel('Amplitude');
subplot(1, 2, 2);
semilogx(freq2,spectrum2)
grid on;
title('SSB Spectrum with 1kHz Tone');
xlabel('Frequency Offset (Hz)'); ylabel('Phase Noise (dBc/Hz)');
return

Best Answer

I’m not sure what you mean by ‘different plots’. Here, figure(1) uses the subplot function, and figure(2) uses the hold function. One of these should work for you:
fidi = fopen('data_mod.txt', 'rt');
vc = textscan(fidi, '%f%f%f%f', 'CollectOutput',1);
v = cell2mat(vc);
% Frequency vector
freq1 = v(:,1);
freq2 = v(:,3) .* (~isnan(v(:,3)));
freqx = [min([freq1; freq2]) max([freq1; freq2])];
% Phase noise vector
spectrum1 = v(:,2);
spectrum2 = v(:,4) .* (~isnan(v(:,4)));
figure(1)
subplot(2,1,1)
plot(freq1, spectrum1)
axis([freqx, ylim])
grid
subplot(2,1,2)
plot(freq2, spectrum2)
axis([freqx, ylim])
grid
figure(2)
plot(freq1, spectrum1)
hold on
plot(freq2, spectrum2)
hold off
axis([freqx, ylim])
grid