1

Having some trouble declaring functions within my script:

%Read the raw audio data

refData = wavread('file1.wav');
userData = wavread('file2.wav'); 

% I want to continue writing my "main" function here, and call the below functions

%%%%%%%%%%%%%
% Functions %
%%%%%%%%%%%%%

%Vector x
function preEmphasis(x)
    alpha = 0.95;
    len = length(x);
    for i=1:len
        x_i = x(i);
        x_iMinus1 = x(i-1);
        x(i) = x_i - alpha*x_iMinus1;
    end
end

%Vector x
function normalization(x)
    maxVal = max(abs(x));
    x = x / maxVal;
end

%Vector x; numFrames, frameSize: integers; stepSize: percentage (float, 0.2 -> 0.5 for example)
function Ymatrix = createYmatrix(x, numFrames, frameSize, stepSize)
        Ymatrix = zeros(numFrames, frameSize);
        for i=1:numFrames
            for j=1:frameSize
                Ymatrix(i,j) = x(stepSize*i + j);
            end
        end
end

The words "function" and "end" are highlighted in red as "parse errors". How can I fix this? Thanks.

3 Answers 3

6

You can't declare functions within your main script. You have to create an external m-file and implement your function inside it, like it says in the official documentation:

Any function that is not anonymous must be defined within a file.

(just to be clear, a script does not accept input arguments or return output arguments).

However, you can have local functions declared inside a function m-file. Read more about function declarations in the official documentation.

EDIT: You can Refer to @natan's answer if you're looking for a way to avoid function m-files altogether. He implemented your functions as anonymous functions, which can be declared inside the script file. Good luck!

Sign up to request clarification or add additional context in comments.

6 Comments

"Local functions inside an m-file" Isn't that what I'm trying to do here? At least that's what I would want... tired of this multiple file nonsense for quick scripting lol.
I believe not. I think it's your main script, since it doesn't have a function declaration in its the beginning.
But otherwise what I'm doing with function ... end is correct and I can just move all the functions into another file?
The end is optional in the case of your function. Just create an m-file named "mymainfunc.m", that starts with for example: function y = mymainfunc(x), implement it and copy paste the rest of your functions inside it. Then call mymainfunc from your main script. Note: your m-file must be called like the main function in that file. Also, this question might also clear things up...
If you want "quick" scripting, you can always make a function run in a run.m file, and implement everything inside it. You'd still have to make a main script and invoke run, though... OR just try to manage without functions :)
|
1

In Addition to what Eitan mentioned, here is how to implement an anonymous functions in your case, note that code vectorization is a must. For example, in your case instead of normalization you can write:

normalization = @(x) x./max(abs(x));

and then use it as if it was a function, y=normalization(x)

For preEmphasis:

preEmphasis= @(x) [x(1) x(2:end)-0.95*x(1:end-1)];

Your current code has a bug for the case i=1 so I interpret that as for=2:len instead;

The solution for Ymatrix is a bit ugly (haven't invested to much time vectorizing it nicely), but it should work:

Ymatrix = @(x, numFrames, frameSize, stepSize) ...
          ones(1,numFrames)'*x(1+stepSize:stepSize+frameSize)+...
          meshgrid(0:stepSize:stepSize*numFrames-1,ones(1,frameSize))';

2 Comments

LOL, I've just suggested it in one of my comments :) +1 anyway. I wonder if createYmatrix can be made into an anonymous function :P
yes Ymatrix can be made to an anonymous function, see my edit
1

Just turn your script into a function; then you can use local and nested functions. Use return values or assignin if you need to get values back in to the base or caller's workspace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.