为什么在写入csv文件时dlmwrite会忽略小数和NaN?

时间:2014-07-17 17:18:17

标签: matlab csv

在描述我的问题之前,让我提供一些背景信息。我最初写了一个函数,它为excel文件写了几个值。由于数据的格式化,我使用xlswrite生成了一个相当不错的电子表格。但是,我需要在没有excel的PC上使用我的程序,所以我调整了我的功能以使用dlmwrite。由于变量不一定具有相同的维度,我编写了一个函数来解析这些维度,以便将它们放入单个矩阵中 - 也就是说,如果它们的长度不相同,则额外的NaN行将被添加。

当我使用小数据集测试此函数时,一切运行良好(小数显示,正在输入NaN等)。这是功能:

function dlmWriteKt(fileNameExtra, time, rawMotorData, usCount, tach, rpmData, angVelocity, r, angAccelLine, maxAccel, inertia, maxT, phaseResist, maxCurrent, kT)


%Generate a filename
      fileName = ['Test_' fileNameExtra '_' num2str(time(1)) '_' num2str(time(2)) '_' num2str(time(3)) '.csv'];

%Construct a matrix whose columns correspond to each variable

    %Store the first column: rawMotorData
       M = rawMotorData;

    %Store the second column: usCount
       M = resolveSize(M, usCount);

    %Store the third column: tach
       M = resolveSize(M, tach);

    %Store the fourth column: rpmData
       M = resolveSize(M, rpmData);

    %Store the fifth column: angVelocity
       M = resolveSize(M, angVelocity);

    %Store the sixth column: r
       M = resolveSize(M, r);

    %Store the seventh column: angAccelLine
       M = resolveSize(M, angAccelLine);

   %Store the eighth column: maxAccel
       M = resolveSize(M, maxAccel);

   %Store the ninth column: inertia
       M = resolveSize(M, inertia);

    %Store the tenth column: maxT
       M = resolveSize(M, maxT);

    %Store the eleventh column: phaseResitst
       M = resolveSize(M, phaseResist);

    %Store the twelth column
       M = resolveSize(M, maxCurrent);

    %Store the thirteenth column: kT
       M = resolveSize(M, kT);

    %Write a csv file
       dlmwrite(fileName, M);
end

function combinedData = resolveSize(currentMatrix, newColumn)
   %This function appends new rows if needed to either the currentMatrix or
   %newColumn in order to concatenate the two.

    %Declare local variables
       numColumnsMatrix = 0;
       numColumnsNew = 0;

    %Determine the number of columns of each matrix
       s = size(currentMatrix);
       numColumnsMatrix = s(1);

       t = size(newColumn);
       numColumnsNew = t(1);

    %Determine how many rows to append to either matrix
       difference = numColumnsMatrix - numColumnsNew;

    %Determine which matrix requires appended rows

        if(difference > 0)
            %Add rows to newColumn
                addRow(1:difference) = NaN;
                newColumn = [newColumn; addRow'];
                combinedData = [currentMatrix newColumn];
                return;

        else if(difference < 0)
                %Add rows to currentMatrix
                    %Determine if there are multiple columns of
                    %currentMatrix.
                        if(s(2) > 1)
                            addRow = ones(abs(difference), s(2));
                            addRow(:,:) = NaN;
                            currentMatrix = [currentMatrix; addRow];
                            combinedData = [currentMatrix newColumn];
                            return;

                        else
                            addRow(1:abs(difference)) = NaN;
                            currentMatrix = [currentMatrix; addRow'];
                            combinedData = [currentMatrix newColumn];
                            return;

                        end
            else
                %The column dimension of both matrices match
                    combinedData = [currentMatrix newColumn];
                    return;
            end
        end
end

当我在命令行中测试它时,此功能正常工作。但是,当我整合到我的大型程序中时,我开始丢失所有小数和NaN。考虑到精度不够高,我将dlmwrite更改为dlmwrite(&#39; fileName&#39;,M,&#39; precision&#39;,&#39;%。6f&#39;)。唯一的变化是24.0改为24.000000!仍然没有小数。此外,所有NaN均表示为0.000000。

我知道问题不在于我的大型计划。除了这个功能,没有任何改变。我验证了传递的参数确实包含小数。在写作期间,我丢失了这些信息。

我已经提供了一些链接,其中包含了我想要查看的一些数据:Spreadsheet Data。考虑到最后一列,第一行应该是0.00478之类的值,下面的所有行都应该是NaN。

任何有建设性的意见都会受到赞赏。

0 个答案:

没有答案