0

次の形式のコンマ区切りファイルがあります。

Col1Name,Col1Val1,Col1Val2,Col1Val3,...Col1ValN,Col2Name,Col2Val1,...Col2ValN,...,ColMName,ColMVal1,...,ColMValN

私の質問は、このファイルをMatlabが行列として処理できるものに変換するにはどうすればよいですか、またこの行列をファイルで使用するにはどうすればよいですか?ファイルをmatlabマトリックス形式にフォーマットしてコピーするためのスクリプト言語を使用できると思いましたが、ファイルはかなり大きい(〜7mb)。

ありがとう!

編集してすみません:

ファイル形式は次のとおりです。

Col1Name;Col2Name;Col3Name;...;ColNName
Col1Val1;Col2Val2;Col3Val3;...;ColNVal1
...
Col1ValM;Col2ValM;Col3ValM;...;VolNValM

実際のデータは次のとおりです。

Press;Temp.;CondF;Cond20;O2%;O2ppm;pH;NO3;Chl(a);PhycoEr;PhycoCy;PAR;DATE;TIME;excel.date;date.time
0.96;20.011;432.1;431.9;125.1;11.34;8.999;134;9.2;2.53;1.85;16.302;08.06.2011;12:01:52;40702;40702.0.5
1;20.011;433;432.8;125;11.34;9;133.7;8.19;3.32;2.02;17.06;08.06.2011;12:01:54;40702;40702.0.5
1.1;20.012;432.7;432.4;125.1;11.34;9;133.8;8.35;2.13;2.2;19.007;08.06.2011;12:01:55;40702;40702.0.5
1.2;20.012;432.8;432.5;125.2;11.35;9.001;133.8;8.45;2.95;1.95;21.054;08.06.2011;12:01:56;40702;40702.0.5
1.3;20.012;432.7;432.4;125.4;11.37;9.002;133.7;8.62;3.17;1.87;22.934;08.06.2011;12:01:57;40702;40702.0.5
1.4;20.007;432.1;431.9;125.2;11.35;9.003;133.7;9.48;4.17;1.6;24.828;08.06.2011;12:01:58;40702;40702.0.5
1.5;19.997;432.3;432.2;124.9;11.33;9.003;133.8;8.5;3.84;1.79;27.327;08.06.2011;12:01:59;40702;40702.0.5
1.6;20;432.8;432.6;124.5;11.29;9.003;133.6;8.57;3.22;1.86;30.259;08.06.2011;12:02:00;40702;40702.0.5
1.7;19.99;431.9;431.9;124.4;11.28;9.002;133.6;8.79;3.7;1.81;35.152;08.06.2011;12:02:02;40702;40702.0.5
1.8;19.994;432.1;432.1;124.4;11.28;9.002;133.6;8.58;3.41;1.84;39.098;08.06.2011;12:02:03;40702;40702.0.5
1.9;19.993;433;432.9;124.6;11.3;9.002;133.6;8.59;3.45;5.53;45.488;08.06.2011;12:02:04;40702;40702.0.5
2;19.994;433;432.9;124.8;11.32;9.002;133.5;8.6;2.76;1.99;50.646;08.06.2011;12:02:05;40702;40702.0.5
4

1 に答える 1

1

前もって行と列の数がわからない場合は、以前のソリューションを使用できません。代わりにこれを使用してください。

7 Mb は大きくなく、小さいです。これが21世紀です。

matlab 行列に読み込むには、次のようにします。

    text = fileread('file.name'); % a string with the entire file contents in it.  7 Mb is no big deal.
    NAMES = {}; % we'll record column names here
    VALUES = []; % this will be the matrix of values

    while text(end) = ','
        text(end)=[]; % elimnate any trailing commas
    end

    commas = find(text==','); % Index all the commas
    commas = [0;commas(:);length(commas)+1] % put fake commas before and after text to simplify loop

    col = 0; % which column are we in

    I = 1;
    while I<length(commas) 
        txt = text(commas(I)+1:commas(I+1)-1);
        I = I+1;
        num = str2double(txt);
        if isnan(num) % this means it must be a column name
            NAMES{end+1,1} = txt;
            col = col+1; % can you believe Matlab doesn't support col++ ???
            row = 1; % back to the top at each new column
            continue % we have dealt with this txt, its not a num so ... next
        end

        % if we made it here we have a number
        VALUES(row,col) = num;
    end

次に、matlab 行列VALUESと、必要に応じてヘッダー名を matlab 形式でmatlab 形式のNAMESファイルに保存できます。

    save('mymatrix.mat','VALUES','NAMES'); % saves matrix and column names to .mat file

ファイルから必要な場合は、次の方法で matlab に戻します。

    load mymatrix.mat; % loads VALUES and NAMES from .mat file

いくつかの制限:

列ヘッダー名にコンマを使用することはできません。

「898.2」など、2 つの数値として読み取れる列に「名前を付ける」ことはできません。数値として読み取られます。

列の長さが異なる場合、短い列には最長の列の長さまでゼロが埋め込まれます。

それは私が考えることができるすべてです。

于 2012-06-22T01:33:53.433 に答える