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개)
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
Jens Voorpyl
2019년 3월 13일
Guillaume
2019년 3월 13일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!.png)
