Cannot run 16-PSK code in Switch case inside of another switch case

조회 수: 2 (최근 30일)
Here's the 16-PSK modulation code I tried:
kanal = get(handles.popupmenu1,'Value');
switch kanal
case 1
bitt=randi([0 1],12,1);
all_bit=sprintf('%d', bitt);
n_bit=length(all_bit);
sampling_bit=50;
bit=[];
for i=1:length(all_bit)
if all_bit(i)~=dec2bin(0) && all_bit(i)~=dec2bin(1)
set_ok=0;
break
else
set_ok=1;
end
end
for n=1:1:length(all_bit);
if all_bit(n)==dec2bin(0)
bit=[bit 1*zeros(1,sampling_bit)];
elseif all_bit(n)==dec2bin(1)
bit=[bit 1*ones(1,sampling_bit)];
end
end
plot(bit,'LineWidth',1.5);
xlabel('ms');
ylabel('amplitude');
grid on;
axis([0 n_bit*sampling_bit -2 2]);
xticks([0:sampling_bit:n_bit*sampling_bit]);
yticks([-2:1:2]);
ax=gca;
ax.XTickLabelRotation = -90;
freq=100;
amp=1;
cycle=2*pi;
ts = 0.0001:0.0002:0.01;
n_psk=16;
n_bit=length(all_bit);
nb_psk=log2(n_psk);
n_simbol=n_bit/nb_psk;
l_ts=length(ts);
ph=[];
simbol=[];
tempSymbol= [];
modulatedSignal=[];
stddev=1;
urutan_konstelasi_sudut=[0 1 3 2 7 6 4 5 15 14 12 13 8 9 11 10];
for i=1:n_psk/2
for j=i:i
urutan_konstelasi_sudut=[urutan_konstelasi_sudut j];
end
for k=n_psk-i+1:n_psk-i+1
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
end
end
for i=1:n_psk
ph=[ph urutan_konstelasi_sudut(i)/n_psk*cycle];
simbol=[simbol [amp*sin(cycle*freq*ts+ph(i))]];
end
for i = 1:nb_psk:n_bit-nb_psk+1
tempSymbol = [all_bit(i:i+nb_psk-1)];
for j=0:n_psk-1
if tempSymbol == dec2bin(j,nb_psk)%[000]
modulatedSignal = [modulatedSignal simbol(j*l_ts+1:(j+1)*l_ts)];
end
end
tempSymbol = []; %reset tempSymbol
end
axes(handles.axes2);
hold off;
plot(modulatedSignal,'LineWidth',1.5);grid on;
hold on;
legend('sinyal informasi')
end
the code above was executed successfully. but when i put the code in Switch case inside of another switch case like below:
kanal = get(handles.popupmenu1,'Value');
switch kanal
case 1
mod = get(handles.popupmenu2,'Value');
switch mod
case 1
bitt=randi([0 1],12,1);
all_bit=sprintf('%d', bitt);
n_bit=length(all_bit);
sampling_bit=50;
bit=[];
for i=1:length(all_bit)
if all_bit(i)~=dec2bin(0) && all_bit(i)~=dec2bin(1)
set_ok=0;
break
else
set_ok=1;
end
end
for n=1:1:length(all_bit);
if all_bit(n)==dec2bin(0)
bit=[bit 1*zeros(1,sampling_bit)];
elseif all_bit(n)==dec2bin(1)
bit=[bit 1*ones(1,sampling_bit)];
end
end
plot(bit,'LineWidth',1.5);
xlabel('ms');
ylabel('amplitude');
grid on;
axis([0 n_bit*sampling_bit -2 2]);
xticks([0:sampling_bit:n_bit*sampling_bit]);
yticks([-2:1:2]);
ax=gca;
ax.XTickLabelRotation = -90;
freq=100;
amp=1;
cycle=2*pi;
ts = 0.0001:0.0002:0.01;
n_psk=16;
n_bit=length(all_bit);
nb_psk=log2(n_psk);
n_simbol=n_bit/nb_psk;
l_ts=length(ts);
ph=[];
simbol=[];
tempSymbol= [];
modulatedSignal=[];
stddev=1;
urutan_konstelasi_sudut=[0 1 3 2 7 6 4 5 15 14 12 13 8 9 11 10];
for i=1:n_psk/2
for j=i:i
urutan_konstelasi_sudut=[urutan_konstelasi_sudut j];
end
for k=n_psk-i+1:n_psk-i+1
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
end
end
for i=1:n_psk
ph=[ph urutan_konstelasi_sudut(i)/n_psk*cycle];
simbol=[simbol [amp*sin(cycle*freq*ts+ph(i))]];
end
for i = 1:nb_psk:n_bit-nb_psk+1
tempSymbol = [all_bit(i:i+nb_psk-1)];
for j=0:n_psk-1
if tempSymbol == dec2bin(j,nb_psk)%[000]
modulatedSignal = [modulatedSignal simbol(j*l_ts+1:(j+1)*l_ts)];
end
end
tempSymbol = []; %reset tempSymbol
end
axes(handles.axes2);
hold off;
plot(modulatedSignal,'LineWidth',1.5);grid on;
hold on;
legend('sinyal informasi')
end
end
there is an error as follows:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in mod16psk>pushbutton1_Callback (line 186)
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in mod16psk (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)mod16psk('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Did I miss something? Thanks before

채택된 답변

Setsuna Yuuki.
Setsuna Yuuki. 2022년 10월 30일
Hi.
In the second code your use the variable:
mod = get(handles.popupmenu2,'Value');
but this name is a function name.
Change the variable name for other.
mod2 = get(handles.popupmenu2,'Value');
switch mod2
case 1
  댓글 수: 2
Ibnu Darajat
Ibnu Darajat 2022년 11월 11일
After I change the function name into "mod2", I got this error:
>> mod_fsk
Unrecognized function or variable 'mod'.
Error in mod_fsk>pushbutton2_Callback (line 261)
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in mod_fsk (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)mod_fsk('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
>>
Now variable "mod" unrecognized

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by