haibaraxx的个人博客分享 http://blog.sciencenet.cn/u/haibaraxx

博文

Matlat: define function and subfunction

已有 2356 次阅读 2016-8-23 05:10 |个人分类:Matlab|系统分类:科研笔记|关键词:学者| and, function, subfunction

Define a function:

% in_arg stands for input arguments, while out_arg stands for output arguments.

Syntax:

function [out_arg1,out_arg2,...]=function_name(in_arg1,in_arg2,...)

description

end



Define a subfunction (also: local function) into the function (also: main function):

% Only main function can be called from outside (by the Command Window& scripts).

% The input&output arguments within the subfunction is unavailable to the main function and the Command Window. The input&output arguments within the main function is unavailable to the subfunction and the Command Window.

% See e.g. below: we give input arguments to the function myRand, and get a as an output. Then a "value" is copied and passed to M as an input argument of the function sumAllElements, and get summa as an output of sumAllElements. Finally, summa "value" is copied and passed to s as the other output of the function myRand.

% make the function polymorphic by setting variable number of arguments
Two built-in functions:

nargin: returns the number of actual input arguments that the function was called with.

nargout: returns the number of output arguments that the function caller requested.






https://m.sciencenet.cn/blog-3031432-998071.html

上一篇:Matlab: precedence
下一篇:Matlab: script

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-2 07:10

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部