Plot two graphs in the same figure, that have different number of points from text file
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2015-10-21
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
0 个评论
更多回答(1 个)
dpb
2015-10-21
编辑:dpb
2015-10-21
Try
[f1 s1 f2 s2]=textread('data_mod.txt','%f %f %f %f');
f2(f2==0)=nan; s2(s2==0)=nan;
subplot(2,1,1),plot(f1,s1)
subplot(2,1,2),plot(f2,s2)
You can even by the above do
figure
plot([f1 f2], [s1 s2])
because there are the same number of points; simply NaN for the shorter series which plot and friends smartly ignore.
If you only care about treating the two separately, then instead of NaN you can simply shorten the second via--
f2(f2==0)=[]; s2(s2==0)=[];
"Salt to suit" the labels axes limits and all, of course...
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!