define function which uses handles.variables

1 回表示 (過去 30 日間)
sadel
sadel 2011 年 6 月 6 日
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)

採用された回答

Jan
Jan 2011 年 6 月 6 日
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 件のコメント
sadel
sadel 2011 年 6 月 6 日
no, I mean that I have: handles.a=2; handles.b=6; handles.c=1; ....
23 items like the previous.
Jan
Jan 2011 年 6 月 6 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Yoav Livneh
Yoav Livneh 2011 年 6 月 6 日
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.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by