www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/SkipSpecificCharactersInFileExample.m

    %% Skip Specific Characters in File
% Skip specific characters in a sample file, and return only numeric data.

% Copyright 2015 The MathWorks, Inc.


%%
% Create a sample text file containing temperature values.
deg = char(176);
str = ['78' deg 'C 72' deg 'C 64' deg 'C 66' deg 'C 49' deg 'C']
fileID = fopen('temperature.dat','w');
fprintf(fileID,'%s',str);
fclose(fileID);

%%
% Read the numbers in the file, skipping the text, °C. Also return the 
% number of values that |fscanf| reads. The extended ASCII code 176 
% represents the degree sign.  
fileID = fopen('temperature.dat','r');
[A,count] = fscanf(fileID, ['%d' deg 'C'])
fclose(fileID);
%%
% |A| is a vector containing the numeric values in the file. |count|
% indicates that |fscanf| read five values.