How to prevent overwrite the graph in looping?

I want to have three eyediagrams and one graph combine three while loop together. The codes is something like the below:
N=2
while (N<=4)
. . . . . .
eyediagram(........)
if(N==2)
figure;
semilogy(........);
hold on;
semilogy(........);
elseif (N==3)
semilogy(........);
hold on;
semilogy(........);
else
semilogy(........);
hold on;
semilogy(........);
. . . . . . .
N=N+1;
end

댓글 수: 3

KSSV
KSSV 2018년 3월 8일
Instead of plotting in conditionals....you collect the data in conditionals...and plot at once after the loops.
Is that possible do it in the while loop?
KSSV
KSSV 2018년 3월 13일
Yes very much....

댓글을 달려면 로그인하십시오.

 채택된 답변

Birdman
Birdman 2018년 3월 8일

0 개 추천

Define figure and hold on just once before while loop. It would be more efficient:
N=2;
figure;hold on;
while (N<=4)
. . . . . .
eyediagram(........)
if(N==2)
semilogy(........);
semilogy(........);
elseif (N==3)
semilogy(........);
semilogy(........);
else
semilogy(........);
semilogy(........);
. . . . . . .
N=N+1;
end

댓글 수: 8

It will produce about six blank graphs and three unwanted and unknown graphs.
semi_fig = figure();
semi_ax = axes('Parent', semi_fig);
hold(semi_ax, 'on')
for N = 2 : 4
...
eyediagram(.....); %always produces new figure
if N == 2
semilogy(semi_ax, ...)
semilogy(semi_ax, ...)
elseif N == 3
semilogy(semi_ax, ...)
semilogy(semi_ax, ...)
elseif N == 4
semilogy(semi_ax, ...)
semilogy(semi_ax, ...)
end
end
hold(semi_ax, 'off')
@Walter Roberson, the solution that you provided is still producing the same results as above
You are incorrect.
semi_fig = figure();
semi_ax = axes('Parent', semi_fig);
hold(semi_ax, 'on')
x = 1 : 50;
for N = 2 : 4
y = rand(1, 50);
eyediagram(y, 3); %always produces new figure
if N == 2
semilogy(semi_ax, x, y, 'k-');
semilogy(semi_ax, x, sin(y), 'k*');
elseif N == 3
semilogy(semi_ax, x, y, 'b-');
semilogy(semi_ax, x, y.^2, 'b*');
elseif N == 4
semilogy(semi_ax, x, y, 'g-');
semilogy(semi_ax, x, cos(y), 'g*');
end
end
hold(semi_ax, 'off')
This produces 3 eye diagrams and one graph that combines all 6 semilogy.
This is my coding and it doesn't work just like yours.
close all; clear; clc;
semi_fig = figure();
semi_ax = axes('Parent', semi_fig);
hold(semi_ax, 'on')
for M = 2:4 % bit resolutions
Lavg=2^M; % average symbol length
nsym=500; % number of PPM symbols
Lsig=nsym*Lavg; % length of PPM slots
Rb=1e6; % bit rate
Ts=M/(Lavg*Rb); % slot duration
Tb=1/Rb; % bit duration
Dt =0.4;
Drms=Dt*Tb; % RMS delay spread
a=12*sqrt(11/13)*Drms;
nsamp=10; % samples per symbols
Tsamp=Tb/nsamp; % sampling time
K=30*nsamp; % number of channel taps
k=0:K;
h=((6*a^6)./(((k*Tsamp)+a).^7)); % channel impulse response
h=h./sum(h);
EbN0=0:12; % energy per slot
EsN0=EbN0+10*log10(M); % energy per symbol
SNR=10.^(EbN0./10);
for ii=1:length(EbN0)
PPM=generate_PPM(M,nsym);
MF_out=awgn(PPM,EsN0(ii)+3,'measured');
% hard decision decoding
Rx_PPM_th=zeros(1,Lsig);
Rx_PPM_th(find(MF_out>0.5))=1;
[No_of_Error(ii) ser_hdd(ii)]=biterr(Rx_PPM_th, PPM);
% soft decision decoding
PPM_SDD=[];
start=1;
finish=2^M;
for k=1:nsym
temp=MF_out(start:finish);
m=max(temp);
temp1=zeros(1,2^M);
temp1(find(temp==m))=1;
PPM_SDD=[PPM_SDD temp1];
start=finish+1;
finish=finish+2^M;
end
[No_of_Error(ii) ser_sdd(ii)]=biterr(PPM_SDD,PPM);
end
Tx_signal=rectpulse(PPM,nsamp); % Pulse shaping function (rectangular pulse)
channel_output=conv(Tx_signal,h); % channel output
eyediagram(channel_output, 3*nsamp);
axis([-0.5 0.5 0 1]);
% theoretical calculation
Pse_ppm_hard=qfunc(sqrt(M*2^M*0.5*SNR));
Pse_ppm_soft=qfunc(sqrt(M*2^M*SNR));
if(M==2)
semilogy(EbN0,(1/log2(M))*Pse_ppm_hard,'k-','linewidth',2);
semilogy(EbN0,(1/log2(M))*Pse_ppm_soft,'r-','linewidth',2);
elseif (M==3)
semilogy(EbN0,(1/log2(M))*Pse_ppm_hard,'k-X','linewidth',2);
semilogy(EbN0,(1/log2(M))*Pse_ppm_soft,'r-X','linewidth',2);
else
semilogy(EbN0,(1/log2(M))*Pse_ppm_hard,'k--','linewidth',2);
semilogy(EbN0,(1/log2(M))*Pse_ppm_soft,'r--','linewidth',2);
end
grid on
legend('M=2 (HDD)','M=2 (SDD)','M=3 (HDD)','M=3 (SDD)','M=4 (HDD)','M=4 (SDD)');
xlabel('Eb/N0, dB');
ylabel('Bit Error Rate');
Title('Bit error probability curve for PPM modulation');
end
hold(semi_ax, 'off')
Unrecognized function or variable 'generate_PPM'.
Error in idaho (line 30)
PPM=generate_PPM(M,nsym);
Save this in a file named as generate_PPM.m
function PPM=generate_PPM(M,nsym)
% function to generate PPM
% 'M' bit resolution
% 'nsym': number of PPM symbol
PPM=[];
for i= 1:nsym
temp=randint(1,M); % random binary number
dec_value=bi2de(temp,'left-msb'); % converting to decimal value
temp2=zeros(1,2^M); % zero sequence of length 2^M
temp2(dec_value+1)=1; % placing a pulse according to decimal value
% note that in matlab index does not start from zero, so need to add 1;
PPM=[PPM temp2]; % PPM symbol
end
end
You did not follow the structure I posted! When you follow the structure I posted, it works fine.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

2018년 3월 8일

댓글:

2018년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by