1

I am trying to provide two parameters to the fun argument of nlfilter function. I would like to do this by using a handle to the function assign_value I created. This is my function:

function y = assign_value(x, ii)

index= x([1 2 3 4 6 7 8 9]);

if ismember(ii, index)==1
    x(5)= ii; % 'ii' is the cloud object ID
end
y=x;
end

I already red some MATLAB documentation (e.g. 1, 2, 3), and saw some answers (4, 5, etc.), but I still would need a help to solve my specific problem in order to understand how handles to functions work. Here is what I'm trying to do (x is a 9by9 double-class matrix)

ii= 127
y= nlfilter(x, [3 3], @assign_value)

The error I get is:

??? Subscripted assignment dimension mismatch.

Error in ==> nlfilter at 75
      b(i,j) = feval(fun,x,params{:});

Any help would be really appreciated, thanks in advance.

ANSWER

Thanks to Acorbe comments,I finally make it. As my y output of assign_value function was an array, and the fun parameter of nlfilter has to output only scalars, I changed my function to:

function y = assign_value(x, ii)

index= x([1 2 3 4 6 7 8 9]);

if ismember(ii, index)==1
    x(5)= ii; % 'ii' is the cloud object ID
end

y=x(5);

end

And doing:

y= nlfilter(x, [3 3], @(x) assign_value(x, ii));

my result is fine. Thanks again to Acorbe for his precious contribution.

Community
  • 1
  • 1
umbe1987
  • 2,894
  • 6
  • 35
  • 63

1 Answers1

1

If I understand your question correctly you want to produce a one-variable version of your function assign_value parametrized by the value of ii. This function will operate the local filtering procedure when called by nlfilter.

Function handles as you were saying can help; specifically you can define

 my_ii = 127;
 assign_value_parametric = @(x) assign_value(x,my_ii);

and use it as

 y= nlfilter(x, [3 3], assign_value_parametric).

The lambda function assign_value_parametric depends upon one single dependent variable (x) since the parameter ii has been fixed once and for all.


In general, consider that this allows a remarkable series of mechanisms.

In particular when you return the function handle outside the function where it has been defined, the parameters it depends upon are automatically shadowed, nonetheless, they are implicitly used when the function handle is called.


EDIT: further comments on your filtering kernel and on the error you get.

I am afraid the kernel you designed does not produce the behavior you need. Consider that a kernel should return a scalar value which is the filter output at point x of a given filtering window around x. In your case you are always returning the original filtering window, possibly with a value changed.

Acorbe
  • 8,367
  • 5
  • 37
  • 66
  • Thanks for the fast response. Anyway, I still get the same error. I also tried with "y= nlfilter(x, [3 3], @(x) assign_value(x,ii))", but nothing changed. As I define the "ii" variable (see code in my answer) I think I could also skip creating any other "my_ii-like" variables, is it correct? But, I tried your code to test whether it works or not, but unfortunately it doesn't. – umbe1987 Mar 07 '14 at 10:40
  • 1
    I guess your problem is that you have to return a scalar value, whilst you are returning an array. E.g. `y=x` is a possible returned value, although is `x` is an array. – Acorbe Mar 07 '14 at 10:48
  • Right, my "assign_value" finally outputs an array (as the final row of the function before the "end" specifies "y=x" while my 'x' is a 9by9 array. I will modify my function and hopefully have my code to work (and I will keep this page up to date). – umbe1987 Mar 07 '14 at 11:01
  • Thanks, I finally make it. I updated my question. How would You suggest to close this thread? – umbe1987 Mar 07 '14 at 11:16
  • @umbe1987, Well, usually, when one is satisfied with the answer, he accepts it and the thread is closed. – Acorbe Mar 07 '14 at 13:28