define function which uses handles.variables

1 view (last 30 days)
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)

Accepted Answer

Jan
Jan on 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 Comments
sadel
sadel on 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 on 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.

Sign in to comment.

More Answers (1)

Yoav Livneh
Yoav Livneh on 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.

Categories

Find more on Structures in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!