MATLAB Practice 2 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy
Function MATLAB Practice 2 – MATLAB Introduce © 2012, SNU CSE Biointelligence Lab.,
Function MATLAB functions Multiple input arguments Multiple returns void input arguments void returns © 2012, SNU CSE Biointelligence Lab., function [output1, output2] = foo(input1, input2, input3) Statements end function foo Statements end
Function In function statement You can use ‘return’ keyword but it’s not mandatory You must declare variable or matrix that has same name as outputs before return or end of function. © 2012, SNU CSE Biointelligence Lab., function [output1, output2] = foo(input1, input2, input3) Statements return; end function [output1, output2] = foo(input1, input2, input3) …. output1 = 1; output2 = 2; …. return; end
Function Create function_name.m in your MATLAB © 2012, SNU CSE Biointelligence Lab., Right Click on Browser Menu Or, just CTRL+N to call empty document.
Function Create function_name.m in your MATLAB © 2012, SNU CSE Biointelligence Lab., filename must be same as function name. (in our case, this file must be saved name as foo.m)
Functions One function_name.m must have one primal function which name is same as file name. One function_name.m can has multiple sub- functions which located under primal functions. © 2012, SNU CSE Biointelligence Lab.,
Functions © 2012, SNU CSE Biointelligence Lab.,
Functions © 2012, SNU CSE Biointelligence Lab., Scope of variables Scope of MATLAB is like C/C++ or JAVA Generally, every internal function variables – exclude return values - in function are removed when function is returned. But, with persistent keyword, internal function variables will remain after function returned.
Useful Tips help function_name | command_name show the description of function or command. lookfor keywords This command takes some time. © 2012, SNU CSE Biointelligence Lab.,