3

我正在尝试将数据写入 .txt 文件。每个文件大约 170MB(写入数据后)。

我正在使用 octave 的 fprintf 函数,使用 '%.8f' 将浮点值写入文件。但是,我注意到一个非常奇怪的错误,因为某些文件中的条目子集已损坏。例如,文件中的其中一行是:

0.43529412,0.}4313725,0.43137255,0.33233533,...

那个“}”应该是“4”。现在 octave 的 fprintf 首先是如何用 '%.8f' 选项写出那个 "}" 的呢?出了什么问题?

另一个例子是,

0.73289\8B987,...

那个“\8B”是怎么到那里的?

我必须处理一个非常大的数据集,总共有 3.6 亿个点。某些文件中的子集行中的此错误已成为一个大问题。是什么导致了这个问题?

此外,这种损坏不是随机发生的。例如,如果一个文件有 110 万行,其中每一行对应于一个表示数据实例的向量,那么问题发生在最多 100 行中,并且这 100 行聚集在一起。例如,这些分布从第 8000 行到第 8150 行,但并非在 100 个损坏的行中,前 50 个位于第 10000 行附近,其余位于第 20000 行。它们总是形成一个集群。

注意:下面的代码是负责提取数据并将其写入文件的代码块。代码中的一些变量,如 K_Cell 已经在较早的时候计算过,并且在数据写入过程中几乎没有任何作用。

mf = fspecial('gaussian',[5 5], 2);
fidM = fopen('14_01_2016_Go_AeossRight_ClustersM_wLAMRD.txt','w');
fidC = fopen('14_01_2016_Go_AeossRight_ClustersC_wLAMRD.txt','w');
fidW = fopen('14_01_2016_Go_AeossRight_ClustersW_wLAMRD.txt','w');
kIdx = 1;
featMat = [];

% - Generate file names to print the data to
featNo = 0;
fileNo = 1;
filePath = 'wLRD10_Data_Road/featMat_';
fileName = [filePath num2str(fileNo) '.txt'];
fidFeat = fopen(fileName, 'w');

% - Compute the global means and standard deviations
gMean = zeros(1,13);        % - Global mean
gStds = zeros(1,13);        % - Global variance
gNpts = 0;                  % - Total number of data points
fidStat = fopen('wLRD10_Data_Road/featStat.txt','w');

for i=1600:10:10000
    if (featNo > 1000000)
        % - If more than 1m points, close the file and open new one
        fclose(fidFeat);

        % - Get the new file name
        fileNo = fileNo + 1;
        fileName = [filePath num2str(fileNo) '.txt'];
        fidFeat = fopen(fileName, 'w');
        featNo = 0;
    end

    imgName = [fAddr num2str(i-1) '.jpg'];
    img = imread(imgName);

    Ir = im2double(img(:,:,1));
    Ig = im2double(img(:,:,2));
    Ib = im2double(img(:,:,3));
    imgR = filter2(mf, Ir);
    imgG = filter2(mf, Ig);
    imgB = filter2(mf, Ib);

    I = im2double(img);
    I(:,:,1) = imgR;
    I(:,:,2) = imgG;
    I(:,:,3) = imgB;

    I = im2uint8(I);

    [Feat1, Feat2] = funcFeatures1(I);
    [Feat3, Feat4] = funcFeatures2(I);
    [Feat5, Feat6, Feat7] = funcFeatures3(I);
    [Feat8, Feat9, Feat10] = funcFeatures4(I);
    ids = K_Cell{kIdx};
    pixVec = zeros(length(ids),13);             % - Get the local image features

    for s = 1:length(ids)                       % - Extract features
        pixVec(s,:) = [Ir(ids(s,1),ids(s,2)) Ig(ids(s,1),ids(s,2)) Ib(ids(s,1),ids(s,2)) Feat1(ids(s,1),ids(s,2)) Feat2(ids(s,1),ids(s,2)) Feat3(ids(s,1),ids(s,2)) Feat4(ids(s,1),ids(s,2)) ... 
                   Feat5(ids(s,1),ids(s,2)) Feat6(ids(s,1),ids(s,2)) Feat7(ids(s,1),ids(s,2)) Feat8(ids(s,1),ids(s,2))/100 Feat9(ids(s,1),ids(s,2))/500 Feat10(ids(s,1),ids(s,2))/200];
    end

    kIdx = kIdx + 1;

    for s=1:length(ids)
        featNo = featNo + 1;
        fprintf(fidFeat,'%d,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f\n', featNo, pixVec(s,:));
    end

    % - Compute the mean and variances
    for s = 1:length(ids)
        gNpts = gNpts + 1;
        delta = pixVec(s,:) - gMean;
        gMean = gMean + delta./gNpts;

        gStds = gStds*(gNpts-1)/gNpts + delta.*(pixVec(s,:) - gMean)/gNpts;
    end
end

注意代码块:

for s=1:length(ids)
    featNo = featNo + 1;
    fprintf(fidFeat,'%d,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f\n', featNo, pixVec(s,:));
end

是将数据点写入文件的代码的唯一部分。

较早的代码块,

if (featNo > 1000000)
    % - If more than 1m points, close the file and open new one
    fclose(fidFeat);

    % - Get the new file name
    fileNo = fileNo + 1;
    fileName = [filePath num2str(fileNo) '.txt'];
    fidFeat = fopen(fileName, 'w');
    featNo = 0;
end

当当前打开的文件超过 100 万个数据点的限制时,打开一个新文件以将数据写入其中。

此外,请注意

   pixVec

变量不能包含除浮点数/双精度值以外的任何内容,否则八度将引发错误。

4

0 回答 0