- Any special reason not to use Anonymous Functions?
- Which Matlab release do you use?
How to define a variable?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dear all,
I have this code:
prompt = {'Type the value j:'};
dlg_title = 'Create your own model';
num_lines = 1;
j = 0.5;
def = {'0.5'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
imdl = mk_common_model('d2d1c',16);
img_1 = mk_image(imdl);
figure
show_fem(img_1);
img_2 = img_1;
['x-' num2str(j) '.^2+ (y- 0.5) .^2<0.1^2, ''x,' 'y,' 'z']
select_fcn = inline('(x-j).^2+(y-0.5).^2<0.1^2','x','y','z')
img_2.elem_data = 1 + elem_select(img_2.fwd_model, select_fcn);
figure
show_fem(img_2);
vh = fwd_solve(img_1);
vi = fwd_solve(img_2);
I have a problem with variable j. I want to change my inline function, for example user enters j = 0.1 so my inline function should look like (x-0.1).^2+(y-0.5).^2<0.1^2','x','y','z'. But Command Window returns this:
ans =
x-0.5.^2+ (y- 0.5) .^2<0.1^2, 'x,y,z
select_fcn =
Inline function:
select_fcn(x,y,z) = (x-j).^2+(y-0.5).^2<0.1^2
Does anyone know where is the mistake?
Thank you for your ideas.
8 comentarios
Stephen23
el 31 de En. de 2015
Editada: Image Analyst
el 31 de En. de 2015
And what kind of an object is img_2.fwd_model ?
At this point I think your best bet would be to start using the debugger , and set a breakpoint inside the Anonymous function.
You could also try this anonymous function instead:
select_fcn = @(varargin)disp(nargin);
Just to see how many inputs it is getting. But the debugger is going to be your best tool for solving this.
Respuestas (1)
per isakson
el 1 de Feb. de 2015
Editada: per isakson
el 1 de Feb. de 2015
"[...]changing variable p whichever user inserts"   I assume "*z*" is a highlighted input variable. However, I cannot find any comment on "*z*".
The anonymous function (function handle) includes a "snapshot" of the current workspace of the moment when it is created. In your case p with the value 0.5 is stored in select_fcn. Later changes of the value of p doesn't doesn't affect select_fcn.
Proposal: replace
select_fcn = @(x,y, *z* ) (x-p).^2+(y-0.5).^2<0.1^2;
by
select_fcn = @(x,y,p) (x-p).^2+(y-0.5).^2<0.1^2;
 
"Error in elem_select (line 57) memb_frac = mean( feval(select_fcn,x,y,z), 2);"   Comments:
- feval(select_fcn,x,y,z) select_fcn is defined with two input arguments, @(x,y)
- feval is not required to evaluate an anonymous function. Replace feval(select_fcn,x,y,z)) by select_fcn(x,y,z)
2 comentarios
per isakson
el 1 de Feb. de 2015
Editada: per isakson
el 1 de Feb. de 2015
What is the intended role of z in
select_fcn = @(x,y,z) (x-p).^2+(y-0.5).^2<0.1^2;
z is input, but not used. Why?
I looked at the EIDORS page, but that didn't help much. I didn't give it the time needed. My conclusions:
- EIDORS is a large toolbox, which is 10++ years old. Anonymous functions didn't exist in Matlab at that time. (However, the current release is only a year old.)
- You try to call the function elem_select with a set of input arguments that doesn't honor the documentation. No surprise an error is thrown.
- You did not provide this context in your question and thus it was hard to give useful answers.
"because I can´t see it in my code"   That's because elem_select (The name is in the error message.) is a function in the EIDORS toolbox.
 
It is not a good idea to modify code in the EIDORS toolbox. Thus, it remains to adhere to the documentation of EIDORS. (We may trust The MathWorks that inline can be replaced by anonymous functions.) The definition of the anonymous function must be like
select_fcn = @(x,y,z) (x-0.5).^2+(y-0.5).^2<0.1^2;
EIDORS doesn't say one may add an extra input argument. That's mean one cannot.
 
One solution is to redefine the anonymous function for each new value of p.
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!