1

我正在尝试读取具有以下重复格式的文件(但由于数据太长,即使是第一次重复,我也已经删除了数据):

1.00 'day' 2011-01-02
'Total Velocity Magnitude RC - Matrix' 'm/day'
    0.190189     0.279141     0.452853      0.61355     0.757833     0.884577 
    0.994502      1.08952      1.17203      1.24442      1.30872      1.36653 
     1.41897      1.46675      1.51035      1.55003      1.58595      1.61824

这是原始文件

在我的一些数据文件中,有几天是十进制数字。在那种情况下,我只是得到小数点前的数字,没有小数点。我正在使用下面显示的代码来读取和存储。我怎样才能在中包含小数部分days例如,我希望days存储 2.2 而不仅仅是 2。

fid = fopen(file_name); % open the file

dotTXT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it as string ('%s') into one big array, row by row
dotTXT_fileContents = dotTXT_fileContents{1};
fclose(fid); %# don't forget to close the file again
% str2match = '''Total Velocity Magnitude RC - Matrix'' ''m/day''';

%# find rows containing 'Total Velocity Magnitude RC - Matrix' 'm/day'
data_starts = strmatch(str2match,...
    dotTXT_fileContents); % data_starts contains the line numbers wherever 'Total Velocity Magnitude RC - Matrix' 'm/day' is found
nDataRows=data_starts(2)-data_starts(1);
ndata = length(data_starts); % total no. of data values will be equal to the corresponding no. of '**  K' read from the .txt file

%# loop through the file and read the numeric data
for w = 1:ndata
    %# read lines containing numbers
    tmp_str = dotTXT_fileContents(data_starts(w):data_starts(w)+nDataRows-2); % stores the content from file dotTXT_fileContents of the rows following the row containing 'Total Velocity Magnitude RC - Matrix' 'm/day' in form of string
    %# convert strings to numbers
    y = cell2mat( cellfun(@(z) sscanf(z,'%f'),tmp_str,'UniformOutput',false)); % store the content of the string which contains data in form of a character
    %# assign output
    data_matrix_column_wise(:,w) = y; % convert the part of the character containing data into number

    %# reading date in days passed since beginning of simulation
    days_str = dotTXT_fileContents(data_starts(w)-1);
    days_str = days_str{1};
    days(w) = sscanf(days_str, '%d');
end
4

2 回答 2

2

days(w) = sscanf(days_str, '%d');days(w) = sscanf(days_str, '%f');

于 2012-04-12T07:24:04.943 回答
1

这比我预期的要长一点。您可能希望以不同的方式读取文件。以下代码逐行处理文件,因此在 while 循环结束时,您可以将 daynum 及其数据放在 allnums 中:

filename = 'single_phase_flow.txt';
fid = fopen(filename);

while 1
    % first line: 1.00 'day' 2011-01-02
    tline = fgetl(fid);
    if ~ischar(tline),   break,   end;
    strs = regexp(tline, '\s', 'split');

    daynum = sscanf(strs{1}, '%f');
    daystr = strs{2};
    datestr = strs{3};

    % second line: 'Total Velocity Magnitude RC - Matrix' 'm/day'
    tline = fgetl(fid);
    if ~ischar(tline),   break,   end;
    if (~regexp(tline, 'Total Velocity'))
        disp('expected Total Velocity line');
        break;
    end

    % now read in numbers until we get to a blank line
    allnums = [];
    gotline = 1;
    while gotline
        tline = fgetl(fid);
        if ~ischar(tline)
            gotline = NaN;
        end
        if (length(tline) == 0)
            gotline = 0;
        else
            % read in more numbers (not especially efficient)
            nums = sscanf(tline, '%f');
            allnums = [allnums; nums];
        end
    end

    % here is the data, you'll need to put it somewhere yourself now
    disp(daynum);
    disp(length(allnums));
end

fclose(fid);
于 2012-04-12T06:06:53.320 回答