How to define a variable?
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
per isakson
el 30 de En. de 2015
Editada: per isakson
el 30 de En. de 2015
Doc says:
inline will be removed in a future release.
Use Anonymous Functions instead.
- Any special reason not to use Anonymous Functions?
- Which Matlab release do you use?
Use Anonymous Functions instead of inline.
Also note that you should avoid using j and i as the names of variables, as these are the names of the inbuilt imaginary unit .
Veronika
el 31 de En. de 2015
Editada: per isakson
el 1 de Feb. de 2015
Stephen23
el 31 de En. de 2015
It really depends on the function elem_select, which I could not find any references to online or within my own MATLAB. Did you write this function yourself, or obtain it from someone, or are you using a third-party toolbox?
There are several possibilities:
- perhaps it requires a z input as well as the x and y inputs. You will need to check the source/documentation for this function to check this.
- maybe the two standard callback inputs hObject and eventdata are somehow conflicting with the inputs that you are defining.
Can you tell use something about elem_select ? Or upload it in a new comment?
Veronika
el 31 de En. de 2015
Veronika
el 31 de En. de 2015
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.
Veronika
el 31 de En. de 2015
Editada: Image Analyst
el 31 de En. de 2015
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
Veronika
el 1 de Feb. de 2015
Editada: per isakson
el 1 de Feb. de 2015
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.
Categorías
Más información sobre Lighting, Transparency, and Shading en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
