1
votes

I've have functions in Matlab that takes two arguments, an image and a kernel and does various image processing applications on it. The signature is Gen_Filt(kernel,img) for various filtering operations. I'm trying to create a second function that closes over the filterings operation with a specific kernel so it can do a particular operation with images.

function [closure] =Close_Over(General,kernel)
    function[out_img]=inner(img)
        
        out_img=General(kernel,img);
    
    end
closure=@inner;


end

So this will take a specific type of filtering operation (Gen_Filt signature) and a specific kernel and return a function handler with the kernel bound to that specific filtering operation. However, it seems to call the filtering operation function and gives the following error:

Error in Gen_Filt (line 61)

out_img=conv2(img,kernel,'same');

which makes me think it's trying to evaluate the filtering operation Gen_Filt before I can call the handler with a specific image argument.

Do I understand the problem correctly and/or am I making a mistake?

1
What is Gen_Filt? Your code snipped doesn’t even reference it! And what is the actual error message? Please read minimal reproducible example, then edit your question accordingly. It’s hard to understand exactly what you did and where the error is generated with the information you’ve given. - Cris Luengo
Gen_Filt is a function I made that takes two parameters, (kernel,img) and it just does the convolution as shown with conv2. That’s the only line in the function because I was just testing it out. I wasn’t expecting it to be called. - Colin Hicks
OK, but that doesn't explain how you call this function, or what the exact error message is that you get (you only share the part of the error message that says where it happens, the part that describes the error itself is not there!). - Cris Luengo
For example, maybe you call Close_Over(Gen_Filt,my_kernel), in which case you are trying to call Gen_Filt. In MATLAB, writing Gen_Filt is the same as writing Gen_Filt(). You would need to call Close_Over(@Gen_Filt,my_kernel). Please edit your question to include a minimal reproducible example, then we can see what the actual problem was. - Cris Luengo
By the way, you can create the same closure using a simpler syntax: my_filter = @(img)Gen_Filt(kernel,img), with kernel known in the current workspace. Then, my_filter(some_image) will apply the Gen_Filt function with kernel as it existed when you created the closure (which is called anonymous function in MATLAB). - Cris Luengo

1 Answers

1
votes

As others have mentioned there are problems with the code you have provided. But, for purposes of answering the title of the post, here is how you create a closure, using nested functions to capture variables in MATLAB.

function fun = createPlusOneFunc

    x = 0; % x is captured
    fun = @inner

    function out = inner(y)
        x = x+y;
        out = x;
    end

end

>> fun = createPlusOneFunc();
>> out = fun(1) % Increments captured variable state by 1.
>> out = fun(2) % Increments captured variable state by 2.