Borrar filtros
Borrar filtros

define function which uses handles.variables

3 visualizaciones (últimos 30 días)
sadel
sadel el 6 de Jun. de 2011
Hi all!
how can I define a function? I have defined 23 handles.variables in my gui and wanna make a function which uses all these variables.I don't know how to define this. Is this correct?
function calcu(a,b,c,q,w,e,r,t,z,u,i,o,p,...
s,d,f,g,h,j,k,l,m,n,handles)

Respuesta aceptada

Jan
Jan el 6 de Jun. de 2011
What do you mean by "23 handles.variables"? Did you create 23 fields in the struct "handles"? Then:
function calcu(handles)
should be enough.
  2 comentarios
sadel
sadel el 6 de Jun. de 2011
no, I mean that I have: handles.a=2; handles.b=6; handles.c=1; ....
23 items like the previous.
Jan
Jan el 6 de Jun. de 2011
Exactly what I have written: The struct "handles" has 23 fields and can be used as single input. You can access the fields as e.g. "disp(handles.a)" insider the function.
I suggest to read the "Getting Started" chapters of the documentation, which explains this basic syntax.

Iniciar sesión para comentar.

Más respuestas (1)

Yoav Livneh
Yoav Livneh el 6 de Jun. de 2011
It is better to send all your 23 variables as one struct, both in terms of neater code and the time and memory it takes to allocate all those new variables into the function's workspace. You can then allocate the various fields into variables if you plan to use them inside the function. For example:
% call the function like this:
result = myfun(handles);
% the function:
function myfun(handles)
% allocate locally whatever you want (not necessary but can be useful)
a = handles.a;
b = handles.b;
% etc. for all required fields
end
Note that if you plan to use each field just once it is better not to create a new variable inside the function's workspace. However for multiple uses it is better, both in time consumption and neatness of your code, to create a local variable.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by