毕业设计——人脸检测——004 学习别人代码——《matlab读取一个目录下的所有图片》

学习warmyellow的代码——《matlab读取一个目录下的所有图片》

 http://blog.csdn.net/warmyellow/article/details/6288670 

function -Declare function

Syntax

function [out1, out2, ...] = myfun(in1, in2, ...)

Description

function [out1, out2, ...] = myfun(in1, in2, ...) declares the function myfun, and its inputs and outputs. The function declaration must be the first executable line of any MATLAB function.

The existing commands and functions that compose the new function reside in a text file that has a .m extension to its filename.

MATLAB program files can be either scripts or functions. Scripts are simply files containing a sequence of MATLAB statements. Functions make use of their own local variables and accept input arguments.

The name of a MATLAB function file begins with an alphabetic character and has a filename extension of .m. The file name, less its extension, is what MATLAB searches for when you try to use the script or function.

The first line of a function defines the syntax with which you call it. The name you give to a function, as defined in the first line, should be the same as the name of the file containing the function, but without the .m extension.

The variables within the body of the function are all local variables.

subfunction,visible only to the other functions in the same file, is created by defining a new function with the function keyword after the body of the preceding function or subfunction. Subfunctions are not visible outside the file where they are defined.

You can terminate any type of function with an end statement, but doing so is not required unless the file containing the function also contains one or more nested functions. Within this file, every function (including primary, nested, private, and subfunctions) must be terminated with an end.

Functions normally return when the end of the function is reached. Use a return statement to force an early return.

When MATLAB does not recognize a function by name, it searches for a file of the same name on disk. If the function is found, MATLAB compiles it into memory for subsequent use. The section Determining Which Function Gets Called in the MATLAB Programming Fundamentals documentation explains how MATLAB interprets variable and function names that you enter, and also covers the precedence used in function dispatching.

When you call a function from the command line or from another function, MATLAB parses the function and stores it in memory. The parsed function remains in memory until cleared with the clear command or you quit MATLAB. The pcode command performs the parsing step and stores the result on the disk as a P-file to be loaded later.

Examples

Example 1

The existence of a file on disk called stat.m containing this code defines a new function called stat that calculates the mean and standard deviation of a vector:

function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));

Call the function, supplying two output variables on the left side of the equation:

[mean stdev] = stat([12.7 45.4 98.9 26.6 53/1])
mean =
   47.3200
stdev =
   29.4085

Example 2

avg is a subfunction within the file stat.m:

function [mean,stdev] = stat2(x)
n = length(x);
mean = avg(x,n);
stdev = sqrt(sum((x-avg(x,n)).^2)/n);

function mean = avg(x,n)
mean = sum(x)/n;

Call the function and compare the answer with that of Example 1, above:

[mean stdev] = stat2([12.7 45.4 98.9 26.6 53/1])
mean =
   47.3200
stdev =
   29.4085

nargin -Number of function input arguments

Syntax

nargin
nargin(function_name)

Description

nargin returns the number of input arguments passed in a call to the currently executing function. Use this nargin syntax only in the body of a function.

nargin(function_name) returns the number of input arguments declared for a given function, function_name. If the function has a variable number of input arguments, nargin returns a negative value.

Input Arguments

function_name

function_name is the name of a function, or the name of a function handle that maps to a specific function. Enclose function_name in single quotation marks.

Examples

This example shows portions of the code for a function called myplot, which accepts an optional number of input arguments:

function myplot(x, y, npts, angle, subdiv)
% MYPLOT  Plot a function.
% MYPLOT(x, y, npts, angle, subdiv)
%     The first two input arguments are
%     required; the other three have default values.
 ...
if nargin < 5, subdiv = 20; end
if nargin < 4, angle = 10; end
if nargin < 3, npts = 25; end
 ...
plot(x, y)

nargout -Number of function output arguments

Syntax

nargout
nargout(function_name)

Description

nargout returns the number of output arguments specified in a call to the currently executing function. Use this nargout syntax only in the body of a function.

nargout(function_name) returns the number of outputs declared for a given function function_name. If the function has a variable number of output arguments, nargout returns a negative value.

Input Arguments

function_name

function_name is the name of a function, or the name of a function handle that maps to a specific function. Enclose function_name in single quotation marks.

Examples

This example shows portions of the code for a function called myplot, which accepts an optional number of output arguments:

function [x0, y0] = myplot(x, y, npts, angle, subdiv)
% MYPLOT  Plot a function.
% MYPLOT(x, y, npts, angle, subdiv)
 ...
if nargout == 0
     plot(x, y)
else
     x0 = x;
     y0 = y;
end

strcmp -Compare strings (case sensitive)

Syntax

TF = strcmp(string,string)
TF = strcmp(string,cellstr)
TF = strcmp(cellstr,cellstr)

Description

TF = strcmp(string,string) compares two strings for equality. The strings are considered to be equal if the size and content of each are the same. The function returns a scalar logical 1 for equality, or scalar logical 0 for inequality.

TF = strcmp(string,cellstr) compares a string with each element of a cell array of strings. The function returns a logical array the same size as the cellstr input in which logical 1 represents equality. The order of the input arguments is not important.

TF = strcmp(cellstr,cellstr) compares each element of one cell array of strings with the same element of the other. The function returns a logical array the same size as either cell array input.

Tips

  • The strcmp function is intended for comparison of character data. When used to compare numeric data, it returns logical 0.

  • Use strcmpi for case-insensitive string comparisons.

  • Any leading and trailing blanks in either of the strings are explicitly included in the comparison.

  • The value returned by strcmp is not the same as the C language convention.

  • strcmp supports international character sets.

Input Arguments

string

A single character string or n-by-1 array of strings.

cellstr

A cell array of strings.

Output Arguments

TF

When both inputs are character arrays, TF is a scalar logical 1 or 0. This value is logical 1 (true) if the size and content of both arrays are equal, and logical 0 (false) if it is not.

When either or both inputs is a cell array of strings, TF is an array of logical ones and zeros. This array is the same size as the input cell array(s), and contains logical 1 (true) for those elements of the input arrays that are a match, and logical 0 (false) for those elements that are not.

Examples

Perform a simple comparison of two strings:

strcmp('Yes', 'No')
ans =
     0
strcmp('Yes', 'Yes')
ans =
     1
 

Create two cell arrays of strings and call strcmp to compare them:

A = {'Handle Graphics', 'Statistics';   ...
     '  Toolboxes', 'MathWorks'};

B = {'Handle Graphics', 'Signal Processing';    ...
     'Toolboxes', 'MATHWORKS'};
match = strcmp(A, B)
match =
     1     0
     0     0

The result of comparing the two cell arrays is:

  • match{1,1} is 1 because "Handle Graphics" in A{1,1} matches the same text in B{1,1}.

  • match{1,2} is 0 because "Statistics" in A{1,2} does not match "Signal Processing" in B{1,2}.

  • match{2,1} is 0 because "  Toolboxes", in A{2,1} contains leading space characters that are not in B{2,1}.

  • match{2,2} is 0 because "MathWorks" in A{2,2} uses different letter case than "MATHWORKS" in B{2,2}, and strcmp does a case-sensitive comparison.

 

The following example has three parts. It compares

  • A string to an array of strings.

  • A padded string to a cell array of strings.

  • An unpadded string to a cell array of strings.

Start by creating the necessary data structures.

  1. Cell array of strings –

    Create a 3-element cell array of strings:

    cellarr = { ...
       'There are 10 kinds of people in the world,'; ...
       'those who understand binary math,'; ... 
       'and those who don''t.'};
  2. String array –

    From the cell array, create a string array. The string array contains space characters at the end of rows 2 and 3 for the padding needed to make the array rectangular:

    strarr = char(cellarr)
    strarr =
    There are 10 kinds of people in the world,
    those who understand binary math,         
    and those who don't.                      
    %                   Each line ends here   ^
    
  3. String vector –

    From row 2 of the string array, create a string vector. This string is also padded with spaces at the end:

    strvec = strarr(2,:)
    strvec =
    those who understand binary math,         
    %                 Padded line ends here   ^

Begin the comparisons. Start by comparing the string vector with the string array. When comparing character arrays, strcmp does not do a row-by-row comparison. It compares all of the 1-by-42 strvec with all of the 3-by-42 strarr. Finding them to be different, the answer isfalse and strcmp returns logical 0:

strcmp(strvec, strarr)
ans =
     0

Compare the string vector to the cell array. Even though strvec is essentially the same as row 2 of cellarr, it is not a match because of the space padding in strvec:

strcmp(strvec, cellarr)
ans =
     0
     0
     0

Remove the space padding from the string vector and compare it to the cell array. strcmp compares strvec with each row of the cellarr, finding a match with the second row of the latter:

strcmp(deblank(strvec), cellarr)
ans =
     0
     1
     0

strcat -Concatenate strings horizontally

Syntax

combinedStr = strcat(s1s2, ..., sN)

Description

combinedStr = strcat(s1s2, ..., sN) horizontally concatenates strings in arrays s1s2, ..., sN. Inputs can be combinations of single strings, strings in scalar cells, character arrays with the same number of rows, and same-sized cell arrays of strings. If any input is a cell array, combinedStr is a cell array of strings. Otherwise, combinedStr is a character array.

Tips

  • For character array inputs, strcat removes trailing ASCII white-space characters: space, tab, vertical tab, newline, carriage return, and form-feed. To preserve trailing spaces when concatenating character arrays, use horizontal array concatenation, [s1s2, ..., sN]. See the final example in the following section.

  • For cell array inputs, strcat does not remove trailing white space.

  • When combining nonscalar cell arrays and multi-row character arrays, cell arrays must be column vectors with the same number of rows as the character arrays.

Examples

Concatenate two cell arrays:

a = {'abcde', 'fghi'};
b = {'jkl',   'mn'};

ab = strcat(a, b)

MATLAB returns

ab = 
    'abcdejkl'    'fghimn'
 

Combine cell arrays a and b from the previous example with a scalar cell:

c = {'Q'};
abc = strcat(a, b, c)

MATLAB returns

abc = 
    'abcdejklQ'    'fghimnQ'
 

Compare the use of strcat and horizontal array concatenation with strings that contain trailing spaces:

a = 'hello  ';
b = 'goodbye';

using_strcat = strcat(a, b)
using_arrayop = [a, b]      % Equivalent to horzcat(a, b)

MATLAB returns

using_strcat =
hellogoodbye

using_arrayop =
hello  goodbye

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值