define function which uses handles.variables
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!