Function definitions are not permitted in this context.

help me
Function definitions are not permitted in this context.

1 comentario

VBBV
VBBV el 24 de Abr. de 2024

Note also that your function outputs three arguments , code, xq, sqnr which you need to concatenate [ ] , inside the function

Iniciar sesión para comentar.

Respuestas (2)

DGM
DGM el 10 de Oct. de 2024
OP was running R2014b, so local functions are not allowed in script files.
VBBV
VBBV el 24 de Abr. de 2024
Editada: VBBV el 24 de Abr. de 2024

Rename the file ok.m to uniform_pcm.m and save file. Type the name of function from command window (giving input arguments) and execute.

14 comentarios

VBBV
VBBV el 24 de Abr. de 2024
If the first line in your file starts with keyword function , then it's considered as function file.
Vu
Vu el 24 de Abr. de 2024
Movida: VBBV el 24 de Abr. de 2024
i have try but i not work
Vu
Vu el 24 de Abr. de 2024
Movida: VBBV el 24 de Abr. de 2024
Vu
Vu el 24 de Abr. de 2024
can you help me fix this
VBBV
VBBV el 24 de Abr. de 2024
Editada: VBBV el 24 de Abr. de 2024
Ok, It seems function starts later in your code. So, you can rename back to whatever name the file previously had. i.e. ok.m
After line 5 i.e.
x = cos(..) + sin(...) % something
you need to call the function by its name as below
[code,xq,sqnr] = uniform_pcq(x,64) % not in command window but inside the file
function [code,xq,sqnr] = uniform_pcq(x,M)
% your code
% your code
% ...
end
Then run the file again by green button (run button)
Vu
Vu el 24 de Abr. de 2024
clc ;
clear all;
T=1/20*pi
t=0:T:1;
x = cos(20*pi*t+pi/2) + 3*sin(10*pi*t) + 5*cos(15*pi*t+pi);
function [code,xq,sqnr] = uniform_pcm(x,M)
Nb=log2(M);
Amax =max(abs(x));
delta=2*Amax/(M-1);
Mq=-Amax:delta:Amax;
M1=0:M-1;
xq= zeros (size(x));
xcode = xq;
for k=1:M
ind =find(x > Mq(k) - delta/2 & x<= Mq(k) + delta/2);
xq(ind)= Mq(k);
xcode (ind) = M1(k) ;
end
end
code = de2bi (xcode, Nb, ' left -msb');
[code,xq,sqnr] = uniform_pcm(x,64)
Vu
Vu el 24 de Abr. de 2024
still not work bro
Vu
Vu el 24 de Abr. de 2024
it still have the same error
VBBV
VBBV el 24 de Abr. de 2024
Editada: VBBV el 24 de Abr. de 2024
clc ;
clear all;
T=1/20*pi
T = 0.1571
t=0:T:1;
x = cos(20*pi*t+pi/2) + 3*sin(10*pi*t) + 5*cos(15*pi*t+pi);
[code,xq] = uniform_pcm(x,64)
code = 7x6
0 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 0 0 0 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xq = 1x7
-5.0844 -4.5629 0.9126 8.2132 2.4770 -4.3021 -7.9525
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [code,xq] = uniform_pcm(x,M)
Nb=log2(M);
Amax =max(abs(x));
delta=2*Amax/(M-1);
Mq=-Amax:delta:Amax;
M1=0:M-1;
xq= zeros (size(x));
xcode = xq;
for k=1:M
ind =find(x > Mq(k) - delta/2 & x<= Mq(k) + delta/2);
xq(ind)= Mq(k);
xcode (ind) = M1(k) ;
end
code = de2bi (xcode, Nb, 'left-msb');
[code,xq.'];
end
% what is sqnr variable ... that is not seen anywhere in your code, yet you
% want it as output ?
Vu
Vu el 24 de Abr. de 2024
oh i am forgot
Vu
Vu el 24 de Abr. de 2024
can you show me the image that you code
Vu
Vu el 24 de Abr. de 2024
i don't know why my code not work
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% put this code in script file & name it as ok.m (valid filename)
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
clc ;
clear all;
T=1/20*pi
t=0:T:1;
x = cos(20*pi*t+pi/2) + 3*sin(10*pi*t) + 5*cos(15*pi*t+pi);
[code,xq] = uniform_pcm(x,64) % call the function inside ok.m
% ----------------------------------
% >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% name this file separate as uniform_pcm.m but inside the same folder where
% ok.m is located
% >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function [code,xq] = uniform_pcm(x,M)
Nb=log2(M);
Amax =max(abs(x));
delta=2*Amax/(M-1);
Mq=-Amax:delta:Amax;
M1=0:M-1;
xq= zeros (size(x));
xcode = xq;
for k=1:M
ind =find(x > Mq(k) - delta/2 & x<= Mq(k) + delta/2);
xq(ind)= Mq(k);
xcode (ind) = M1(k) ;
end
code = de2bi (xcode, Nb, 'left-msb');
[code,xq.'];
end
% what is sqnr variable ... that is not seen anywhere in your code, yet you
% want it as output ?
VBBV
VBBV el 23 de Oct. de 2024
Editada: VBBV el 23 de Oct. de 2024

@Vu run the above code, ok.m file given in code. Both files ok.m and uniform_pcm.m need to be in same FOLDER. Since you are using R2014b version, the function definitions are not allowed inside script file as error messages tell. Otherwise check with a higher version and run your code.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

Vu
el 24 de Abr. de 2024

Editada:

el 23 de Oct. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by