define function which uses handles.variables

2 Ansichten (letzte 30 Tage)
sadel
sadel am 6 Jun. 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)

Akzeptierte Antwort

Jan
Jan am 6 Jun. 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 Kommentare
sadel
sadel am 6 Jun. 2011
no, I mean that I have: handles.a=2; handles.b=6; handles.c=1; ....
23 items like the previous.
Jan
Jan am 6 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Yoav Livneh
Yoav Livneh am 6 Jun. 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.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by