How to define a variable?

7 visualizzazioni (ultimi 30 giorni)
Veronika
Veronika il 30 Gen 2015
Modificato: per isakson il 1 Feb 2015
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 Commenti
Stephen23
Stephen23 il 31 Gen 2015
Modificato: Image Analyst il 31 Gen 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.
Veronika
Veronika il 31 Gen 2015
Modificato: Image Analyst il 31 Gen 2015
img_2.fwd_model is figure of the conductivity based on the function (x-p).^2+(y-0.5).^2<0.1^2.
Ok, I try debugger.

Accedi per commentare.

Risposte (1)

per isakson
per isakson il 1 Feb 2015
Modificato: per isakson il 1 Feb 2015
"[...]changing variable p whichever user inserts" &nbsp 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;
&nbsp
"Error in elem_select (line 57) memb_frac = mean( feval(select_fcn,x,y,z), 2);" &nbsp 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 Commenti
Veronika
Veronika il 1 Feb 2015
Modificato: per isakson il 1 Feb 2015
Thank you for your answer.
  1. I rewrote z to p, but nothing changed, still returns me to a value of 0.5 rather than a user specified value.
  2. And for the second error, I do not know where I have to change that select_fcn(x,y,z), because I can´t see it in my code.
per isakson
per isakson il 1 Feb 2015
Modificato: per isakson il 1 Feb 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" &nbsp That's because elem_select (The name is in the error message.) is a function in the EIDORS toolbox.
&nbsp
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.
&nbsp
One solution is to redefine the anonymous function for each new value of p.

Accedi per commentare.

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by