18-3 Ū�� ASCII �ɮ�

fgetl ¨ç¼Æ¥i±N ASCII Àɮתº¤º®e¤¤ªº¬Y¤@¦CŪ¥X¡A¨Ã±N¸Ó¦Cªº ASCII ¤º®e¥HÂন¦r¦ê¶Ç¦^¡C¨Ò¦p¡A¤U¦C«ü¥O¥i±NÀÉ®× mean.m ³v¦C¦L¥X¡G

Example 1: 18-ÀÉ®×Ū¼g/fgetl01.mfid = fopen('mean.m', 'r'); while feof(fid)==0 % feof ´ú¸ÕÀɮ׫ü¼Ð¬O§_¤w¨ì¹Fµ²§ô¦ì¸m line = fgetl(fid); disp(line); endfunction y = mean(x,dim,flag,flag2) %MEAN Average or mean value. % 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(..., MISSING) 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: If X = [1 2 3; 3 3 6; 4 6 8; 4 7 7]; % % then mean(X,1) is [3 4.5 6] and mean(X,2) is [2; 4; 6; 6] % % 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-2014 The MathWorks, Inc. isDimSet = nargin > 1 && ~ischar(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) isnative = (lower(flag(1)) == 'n'); 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 ~isrow(flag) error(message('MATLAB:mean:invalidFlags')); end s = strncmpi(flag, {'omitnan', 'includenan'}, max(length(flag), 1)); if ~isFlag2Set omitnan = s(1); if any(s) flag = 'default'; end else if ~isrow(flag2) error(message('MATLAB:mean:invalidFlags')); 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

°õ¦æ¤W­zµ{¦¡«á¡AMATLAB ·|¥ý¦b¥Ø«e¥Ø¿ý§ä´M mean.m¡A­Y§ä¤£¨ì¡A¦A®Ú¾Ú·j´M¸ô®|¡A§ä¥X mean.m «ü¥Oªº¦ì¸m¡AµM«á¦A±N¨ä¤º®e¤@¦C¤@¦C¦a¦C¥X¡C¡]¬°¸`¬ÙªÅ¶¡¡A¦b¦¹¤£¦C¦L¥Xµ²ªG¡A½ÐŪªÌ¦Û¦æ¸Õ¥Î¦¹½d¨Òµ{¦¡½X¡C¡^­Y­nª¾¹D¦¹ mean.m ©Ò¦bªº¦ì¸m¡A¥i¿é¤J¡uwhich mean¡v§Y¥iÅã¥Ü¦¹ÀɮשҦbªº¸ô®|¡C

fget ¨ç¼Æ©M fgetl ¨ç¼Æ«Ü¬Û¦ü¡A¨âªÌ§¡¥i¥ÑÀÉ®×Ū¨ú¤@¦C¸ê®Æ¡A¨ä®t§O¦b©ó¡Gfgetl ·|±Ë¥h´«¦æ¦r¤¸¡A¦Ó fgets ¨ç¼Æ«h«O¯d´«¦æ¦r¤¸¡C

§Q¥Î fgetl ¨ç¼Æ¡A§Ú­Ì¥i¥H¼ÒÀÀ¦b UNIX ¨t²Î¤Uªº grep «ü¥O¡A¨Ó§ä¥X¥]§t¬Y¤@¯S©w¦r¦êªº¤@¦C¡A¨ä MATLAB ¨ç¼Æ grep.m ¥i¼¶¼g¦p¤U¡G

>> type grep.m function grep(filename, pattern) fid = fopen(filename, 'r'); line_number = 0; while feof(fid) == 0, line = fgetl(fid); matched = findstr(line, pattern); if ~isempty (matched) fprintf('%d: %s \n', line_number,line); end line_number = line_number + 1; end fclose(fid);

¨Ò¦p¡A±ý¦C¥X grep.m ¤¤¥]§t 'matched' ¦r¦êªº¨C¤@¦C¡A¥i¿é¤J¦p¤U¡G

>> grep('grep.m', 'matched')<xmp> <xmp class=code> 6: matched = findstr(line, pattern); 7: if ~isempty (matched)

Hint
­Y­n¶i¦æ§ó½ÆÂøªº¦r¦ê¤ñ¹ï¡A¨Ò¦p­n¦b¤@­ÓÀɮפ¤§ä¥X¡ub ©M d ¤¤¶¡§¨¤@¦Ü¤T­Ó¥À­µ¡vªº­^¤å¦r¡A½Ð°Ñ¦Ò¥»®Ñ©n©f§@¡uMATLAB µ{¦¡³]­p¡G¶i¶¥½g¡v¤¤ªº²Ä¥|³¹¡u³q¥Î¹Bºâ¦¡¡v¡C

­Y¤wª¾ ASCII Àɮתº®æ¦¡¡A±ý¶i¦æ§óºë½TªºÅª¨ú¡A¥i¥Î fscanf ¨ç¼Æ¨Ó±qÀɮפ¤Åª¨ú®æ¦¡¤Æ¤§¸ê®Æ¡A¨ä¨Ï¥Î»yªk¦p¤U¡G

matrix = fscanf(fid, format)

¨ä¤¤ fid ¬O±ýŪ¨ú¤§Àɮתº¿ëÃѽX¡]¥Ñ fopen ²£¥Í¡^¡Aformat ¬O®æ¦¡«ü©w¦r¦ê¡]Format Specifier¡^¡A¥Î¥H«ü©wŪ¤J¸ê®Æªº«¬ºA¡A±`¥Îªº®æ¦¡«ü©w¦r¦ê¦³¤U¦C´XºØ¡G

¨ä¥L¦UºØ®æ¦¡«ü©w¦r¦ê¥i¿é¤J help fscanf ¨Ó±o¨ì¸Ô²Óªº½u¤W»¡©ú¡C¨Ò¦p¡A¦³¤@ ASCII ÀÉ®× test.txt ªº¤º®e¦p¤U¡G

>> type test.txt 1 4 9 16 25 36 49 64 81 100

±ý¨Ï¥Î fscanf «ü¥OŪ¨ú¨ä¤º®e¡A¥i¿é¤J¦p¤U¡G

Example 2: 18-ÀÉ®×Ū¼g/fscanf01.mfid = fopen('test.txt', 'r'); myData = fscanf(fid, '%g'); fclose(fid); myData % Åã¥Ü myData myData = 1 4 9 16 25 36 49 64 81 100

¤W¨Ò¤]Åã¥Ü¤F MATLAB ªº fscanf «ü¥O©M C ªº fscanf «ü¥Oªº³Ì¤j¤£¦P¡GMATLAB ªº fscanf «ü¥O¬O¦V¶q¤Æªº¡]Vectorized¡^¡A¥u­nŪ¤J¸ê®Æªº«¬ºA¥¿½T¡AMATLAB ªº fscanf «ü¥O·|¤@¦A°õ¦æ¡A¨Ã§â©Ò±oµ²ªG¦s©ñ©ó¤@­Ó¦V¶q¨Ã¦^¶Ç¡C

­Y­n­­¨î¶Ç¦^¦V¶qªº¤j¤p¡A©Î¬O§Æ±æ¶Ç¦^¤@¯x°}¡A«h¥i¦b fscanf ¥[¤W²Ä¤T­Ó¿é¤J¤Þ¼Æ¡A¥H«ü©w¶Ç¦^¦V¶q¡]©Î¯x°}¡^ªº¤j¤p¡C¨Ò¦p¡G±ýŪ¨ú test.txt ªº«e 3 µ§¸ê®Æ¡A¥i¿é¤J¦p¤U¡G

Example 3: 18-ÀÉ®×Ū¼g/fscanf02.mfid = fopen('test.txt', 'r'); myData = fscanf(fid, '%g', 3); fclose(fid); myData % Åã¥Ü myData myData = 1 4 9

±ý¨Ï fscanf «ü¥O¶Ç¦^¤@­Ó 3¡Ñ2 ªº¯x°}¡A¥i¿é¤J¦p¤U¡G

Example 4: 18-ÀÉ®×Ū¼g/fscanf03.mfid = fopen('test.txt', 'r'); myData = fscanf(fid, '%g', [3, 2]); fclose(fid); myData % Åã¥Ü myData myData = 1 16 4 25 9 36

Hint
fscanf ¦bŪ¨úÀɮ׮ɡA¬O¤@¦C¤@¦C¶i¦æªº¡C¦ý¦b¶Ç¦^¯x°}®É¡A¬O±N¸ê®Æ¤@¦æ¤@¦æ¦a¶ñ¤J±ý¶Ç¦^ªº¯x°}¡C¡]¦b¥»®Ñ¤¤¡A¡u¦C¡v§Y«ü¡u¾î¦C¡v¡A¡u¦æ¡v§Y«ü¡uª½¦æ¡v¡C¡^

sscanf ¨ç¼Æ©M fscanf ªº¥\¯à«ÜÃþ¦ü¡A°ß¤@¤£¦Pªº¬O¡Asscanf ¨ç¼Æ±q¦r¦ê¡]Strings¡^¤¤Åª¨ú¸ê®Æ¡A¦Ó¤£¬O±qÀɮס]Files¡^¡C¨Ò¦p¡G

Example 5: 18-ÀÉ®×Ū¼g/sscanf01.mstr = num2str([pi, sqrt(2), log10(3)]) retrieved = sscanf(str, '%g') str = 3.1416 1.4142 0.47712 retrieved = 3.1416 1.4142 0.4771


MATLABµ{¦¡³]­p¡G¤Jªù½g