For loop in function doesn't work, but does work outside of it.

Good day!
For a school exercise I need to make a function called 'compressor' that makes a compressor graph used to compress audio. Anyway, this is the code I have (see below). Now I ran into the problem that when I execute 'run the function' in the editor or in the command window I get a lot of values for level and gain and it doesn't stop.
if this is my code: (I only execute the last line after executing the function once)
function [y,G]=compressor(x,CR1,CR2,xc,N);
[nr_samples,nr_channels]=size(x); % find length and number of channels of input
y=zeros(nr_samples,nr_channels); % initialize output with zeros
G=zeros(nr_samples,1); % initialize gain vector with zeros
yc = nthroot(xc, CR1); % compute (xc,yc)
for k=N:N:length(x) % go through signal in steps of N
level= max(x(k-N+1:1:k)); % compute input level at time k
if level>xc % compute gain at time k in case level>xc
gain= level.^((CR1/1)-1);
else % compute gain at time k in case level<=xc
gain= (yc/xc)*(level/xc)^((1/CR2)-1) ;
end
end
y = x * gain % apply gain and compute output
G = gain;
end
[y1,G1] = compressor(logspace(-6,0,1000), 5, 1, 0.1, 1)
1) I get this error:
Error: File: compressor.m Line: 21 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "compressor".)
2) Or it works!
It feels totally random which one it does. Both executing it in the command window and in the editor gives the same results.
If I delete the last ‘end’, and execute the last line, it keeps looping on the calculation of y, the level and the gain.
I have no idea what causes these things and why they seem so random, does anybody see my mistake? The file path and all seem to be right. Also if I copy the code of the function and execute it outside of it everything seems to work fine all of the time.
Thanks in advance!
EDIT:
I have attached the m-file.

답변 (1개)

Guillaume
Guillaume 2019년 3월 13일
편집: Guillaume 2019년 3월 13일
Nothing is ever random on a computer.
Your description is not entirely clear. If I got it wrong, then attach your m file so we know exactly what you're running and what it is called.
Functions within a script must always follow the script. So, if in a m file you have:
%whole content of m file, whose name you haven't specified
function [y,G]=compressor(x,CR1,CR2,xc,N) %note that you don't normally put a semicolon at the end of this line
%content irrelevant
%...
end
[y1,G1] = compressor(logspace(-6,0,1000), 5, 1, 0.1, 1);
Then, yes it is an error, the script part (the bit not in a function) is after the function when it should be before.
Trivally fixed by:
[y1,G1] = compressor(logspace(-6,0,1000), 5, 1, 0.1, 1);
function [y,G]=compressor(x,CR1,CR2,xc,N) %note that you don't normally put a semicolon at the end of this line
%content irrelevant
%...
end
Of course, if you change the original code to
%whole content of m file, whose name you haven't specified
function [y,G]=compressor(x,CR1,CR2,xc,N) %note that you don't normally put a semicolon at the end of this line
%content irrelevant
%...
[y1,G1] = compressor(logspace(-6,0,1000), 5, 1, 0.1, 1);
by removing the end, then the call to compressor becomes part of the function, meaning your function keep calling itself in an infinite recursion.
Documentation on how to put functions into scripts. Note that prior to R2016 (a or b, can't recall) you could not have functions in a script.

댓글 수: 2

Hey! Thanks so much for responding.
I have attached the m-file to the post.
As you said I removed the last line of code and put an end on the end of the function. I can now run the function by typing the last line in the command window instead of in the editor.
You also said that you can put this line:
[y1,G1] = compressor(logspace(-6,0,1000), 5, 1, 0.1, 1);
before the function instead of in the command window (correct me if I'm wrong) and still run it. I have tried that but then I get this error: (see screenshot)
Screenshot (19).png
I don't know if it is because it has the same name as the m-file or not. But I tried to run it with another name, and then it wouldn't work at all.
Do you know why it does this?
Again, thanks so much!
It is indeed because the script has the same name as the function. The script should have a different name. How about renaming the file to run_compressor.m?
You should also take note of all the orange squiglies which is matlab way of telling you that there are potential issues (in this case, missing semi-colons).

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

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

질문:

2019년 3월 13일

댓글:

2019년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by