3.2 M文件

(张德丰数学建模读书笔记)

3.2 M 文件

脚本M文件 函数M文件
不接受参数输入和输出 需要接受
与matlab工作空间共享变量空间 独立的内部变量空间
实现一个相对独立的功能,比如分析,绘图,微分方程 (同)实现单独功能的代码块
需要制定接受输出结果的工作空间变量
通过命令行窗口输入文件名运行
通过积累专业领域应用函数, 组建自己的专业领域工具箱

M文件结构

与主程序文件不同的另一类M文件就是函数文件,它与主程序文件的主要区别又下三点:(此句没懂??)
(1)由function起头,后跟的函数名必须与文件名相同
(2)又输入输出变量(变元),可进行变量传递
(3)除非用global声明,程序中的变量均为局部变量,不保存在工作空间中,
函数文件一般又5部分组成:
(1)函数定义行
(2)H1行
(3)函数帮组文本
(4)函数体
(5)注释

脚本文件

Matlab指令类似DOS命令,而脚本文件类似DOS系统中的.bat批处理文件。脚本文件想一个”包“,简化操作。脚本与Matlab会话共享基本工作空间,主要操作工作空间中的数据,也可以在巩固走空间中产生信的数据。

示例

实例

建立一个命令文件,将变量a,b的值互换
输入>>type mean 显示:
1)函数定义行:function y=mean(x,dim)
function为函数定义的关键字,mean为函数名,y为输出变量,x和dim为输入变量.

类型 操作
多个输出变量 方括号括起
多个输入变量 圆括号
不含输出变量 空括号/省略
例如:function[x,y,z]=sphere(theta,phi,rho);function printresults(x);function[]=printresults(x)

2) H1行 :注释行
帮助文档的第一行,lookfor函数只在H1行内搜索,help函数也可搜出
3)函数帮助文本
从H1行开始到第一个非%行结束,help函数显示
4)函数体
5)注释

>> type mean

function y = mean(x,dim,flag,flag2)
%MEAN   Average or mean value. %H1
%   S = MEAN(X) is the mean value of the elements in X if X is a vector. %函数帮组文档
%   For matrices, S is a row vector containing the mean value of each 
%   column. 
%   For N-D arrays, S is the mean value of the elements along the first 
%   array dimension whose size does not equal 1.
%
%   MEAN(X,DIM) takes the mean along the dimension DIM of X.
%
%   S = MEAN(...,TYPE) specifies the type in which the mean is performed, 
%   and the type of S. Available options are:
%
%   'double'    -  S has class double for any input X
%   'native'    -  S has the same class as X
%   'default'   -  If X is floating point, that is double or single,
%                  S has the same class as X. If X is not floating point, 
%                  S has class double.
%
%   S = MEAN(...,NANFLAG) specifies how NaN (Not-A-Number) values are 
%   treated. The default is 'includenan':
%
%   'includenan' - the mean of a vector containing NaN values is also NaN.
%   'omitnan'    - the mean of a vector containing NaN values is the mean 
%                  of all its non-NaN elements. If all elements are NaN,
%                  the result is NaN.
%
%   Example:
%       X = [1 2 3; 3 3 6; 4 6 8; 4 7 7]
%       mean(X,1)
%       mean(X,2)
%
%   Class support for input X:
%      float: double, single
%      integer: uint8, int8, uint16, int16, uint32,
%               int32, uint64, int64
%
%   See also MEDIAN, STD, MIN, MAX, VAR, COV, MODE.

%   Copyright 1984-2015 The MathWorks, Inc.

isDimSet = nargin > 1 && ~ischar(dim) && ~(isstring(dim) && isscalar(dim));
isFlag2Set = nargin >= 4;

if nargin == 1 || (nargin == 2 && isDimSet) %函数主体

    flag = 'default';
    omitnan = false;

else % nargin >= 3 || (nargin == 2 && ~isDimSet)

    if nargin == 2
        flag = dim;
    elseif nargin == 3
        if ~isDimSet
            flag2 = dim;
            isFlag2Set = true;
        end
    elseif nargin == 4 && ~isDimSet
        error(message('MATLAB:mean:nonNumericSecondInput'));
    end

    if ~isFlag2Set
        flag2 = '';
    end

    [flag, omitnan] = parseInputs(flag, flag2, isFlag2Set);

end

if ~isDimSet
    % preserve backward compatibility with 0x0 empty
    if isequal(x,[])
        y = sum(x,flag)/0;
        return
    end

    dim = find(size(x)~=1,1);
    if isempty(dim), dim = 1; end
end

if ~isobject(x) && isinteger(x)
    % accumulation flag may still be partial
    isnative = strncmpi(flag, 'native', max(1, strlength(flag)));
    if intmin(class(x)) == 0  % unsigned integers
        y = sum(x,dim,flag);
        if (isnative && all(y(:) < intmax(class(x)))) || ...
                (~isnative && all(y(:) <= flintmax))
            % no precision lost, can use the sum result
            y = y/size(x,dim);
        else  % throw away and recompute
            y = intmean(x,dim,isnative);
        end
    else  % signed integers
        ypos = sum(max(x,0),dim,flag);
        yneg = sum(min(x,0),dim,flag);
        if (isnative && all(ypos(:) < intmax(class(x))) && ...
                all(yneg(:) > intmin(class(x)))) || ...
                (~isnative && all(ypos(:) <= flintmax) && ...
                all(yneg(:) >= -flintmax))
            % no precision lost, can use the sum result
            y = (ypos+yneg)/size(x,dim);
        else  % throw away and recompute
            y = intmean(x,dim,isnative);
        end
    end
else
    if omitnan

        % Compute sum and number of NaNs
        m = sum(x, dim, flag, 'omitnan');
        nr_nonnan = size(x, dim) - matlab.internal.math.countnan(x, dim);

        % Divide by the number of non-NaNs.
        y = m ./ nr_nonnan;
    else
        y = sum(x, dim, flag)/size(x,dim);
    end
end

end


function y = intmean(x, dim, isnative)
% compute the mean of integer vector

shift = [dim:ndims(x),1:dim-1];
x = permute(x,shift);

xclass = class(x);
if ~isnative
    outclass = 'double';
else
    outclass = xclass;
end

if intmin(xclass) == 0
    accumclass = 'uint64';
else
    accumclass = 'int64';
end
xsiz = size(x);
xlen = cast(xsiz(1),accumclass);

y = zeros([1 xsiz(2:end)],outclass);
ncolumns = prod(xsiz(2:end));
int64input = isa(x,'uint64') || isa(x,'int64');

for iter = 1:ncolumns
    xcol = cast(x(:,iter),accumclass);
    if int64input
        xr = rem(xcol,xlen);
        ya = sum((xcol-xr)./xlen,1,'native');
        xcol = xr;
    else
        ya = zeros(accumclass);
    end
    xcs = cumsum(xcol);
    ind = find(xcs == intmax(accumclass) | (xcs == intmin(accumclass) & (xcs < 0)) , 1);

    while (~isempty(ind))
        remain = rem(xcs(ind-1),xlen);
        ya = ya + (xcs(ind-1) - remain)./xlen;
        xcol = [remain; xcol(ind:end)];
        xcs = cumsum(xcol);
        ind = find(xcs == intmax(accumclass) | (xcs == intmin(accumclass) & (xcs < 0)), 1);
    end

    if ~isnative
        remain = rem(xcs(end),xlen);
        ya = ya + (xcs(end) - remain)./xlen;
        % The latter two conversions to double never lose precision as
        % values are less than FLINTMAX. The first conversion may lose
        % precision.
        y(iter) = double(ya) + double(remain)./double(xlen);
    else
        y(iter) = cast(ya + xcs(end) ./ xlen, outclass);
    end
end
y = ipermute(y,shift);

end


function [flag, omitnan] = parseInputs(flag, flag2, isFlag2Set)
% Process flags, return boolean omitnan and string flag

    if isInvalidText(flag)
        error(message('MATLAB:mean:invalidFlags'));
    end
    if isstring(flag)
        flag = char(flag);
    end
    s = strncmpi(flag, {'omitnan', 'includenan'}, max(length(flag), 1));

    if ~isFlag2Set
        omitnan = s(1);
        if any(s)
           flag = 'default';
        end
    else
        if isInvalidText(flag2)
            error(message('MATLAB:mean:invalidFlags'));
        end
        if isstring(flag2)
            flag2 = char(flag2);
        end
        s2 = strncmpi(flag2, {'omitnan', 'includenan'}, max(length(flag2), 1));

        % Make sure one flag is from the set {'omitnan', 'includenan'},
        % while the other is from {'default', 'double', 'native'}.        
        if ~xor( any(s), any(s2) )
            error(message('MATLAB:mean:invalidFlags'));
        end

        if any(s) % flag contains 'includenan' or 'omitnan'
            omitnan = s(1);
            flag = flag2;
        else
            omitnan = s2(1);
        end
    end
end

function tf = isInvalidText(str)
tf = (ischar(str) && ~isrow(str)) || ...
     (isstring(str) && ~(isscalar(str) && (strlength(str) > 0)));
end
>> 
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页