フィルターのクリア

Strange behavior of anonymous functions

1 回表示 (過去 30 日間)
Patrick Mboma
Patrick Mboma 2015 年 3 月 13 日
コメント済み: Patrick Mboma 2015 年 3 月 14 日
Dear all,
I have two problems related to anonymous functions:
1-) I create an anonymous function taking several inputs inside a m-file function. Would like the anonymous function to take one input and use the other pre-created inputs from the workspace. for instance,
function f=myfunc(...)
a=rand(10,1); b=rand(10,1); c=rand(10,1); ss=rand(10,1);
f=@(x)a(6,:)*x^2+b(4,:)*x+c(3,:)-ss(5)
I get an error when calling the function I get an error saying that "ss" is undefined for inputs of type double and that I many need some toolbox that I do not have. But then I change the second line in the function to
f=@(x)a(6,:)*x^2+b(4,:)*x+c(3,:)-ss(5,:)
and everything works. Has anyone encountered such a problem? Is there any explanation for that behavior?
2-) After creating the anonymous function, I put it in a structure that I would like to carry around. The structure contains the anonymous function and various other elements. mystruct=struct(...,'anonym',myfunc(...)); I now would like to update the anonymous function inside the structure. In particular, I would like to change the order of input "x" before passing it to the anonymous function. I thought I could just create another anonymous function as follows neworder=10:-1:1; mystruct.anonym=@(x)mystruct.anonym(x(neworder));
This works without any problem. The issue is that when I now inspect the workspace of "mystruct.anonym" by issuing w=functions(mystruct.anonym), w.workspace, it contains an almost interminable loop of mystruct.anonym woven inside the sub-workspaces. This, most certainly, uses some memory. Is there any way to get rid of this behavior?
Thanks,
  3 件のコメント
Patrick Mboma
Patrick Mboma 2015 年 3 月 13 日
this is just a piece of code to illustrate the issue. the actual code is a lot more complicated.
Brendan Hamm
Brendan Hamm 2015 年 3 月 13 日
Also runs perfectly fine for me. I would check the original code and see if maybe you have a typo. Something like:
s = rand(10,1);
f=@(x)a(6,:)*x^2+b(4,:)*x+c(3,:)-ss(5)

サインインしてコメントする。

回答 (1 件)

per isakson
per isakson 2015 年 3 月 13 日
編集済み: per isakson 2015 年 3 月 13 日
  1. If you have the Control or Signal Toolbox, ss as the name for variables used to cause trouble. See ss,Create state-space model, convert to state-space model. I once spent time to learn that:-(
  2. TMW is working on it. Not much we can do. Try to keep the workspace small when creating the anonymous function.
  2 件のコメント
Patrick Mboma
Patrick Mboma 2015 年 3 月 14 日
Hi Per,
I change the variable name and it still does not work. It very much looks to me like a bug in the version of matlab I am using (2014b) but I am not 100 percent sure. I will try and install a different version and see whether something changes.
To summarize the problem, ss (or any other name I choose) is created before the anonymous function. But it just does not appear in the workspace of the anonymous function unless I call it as ss(2,:).
Patrick Mboma
Patrick Mboma 2015 年 3 月 14 日
I have tried a different version of matlab and I get the same results. Although the variable exists in the workspace, somehow matlab must interpret it as a function, I guess...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by