1

nこのファイルは、最初の行がヘッダーで、その後にデータを含む行が続く出力ファイルです。この最初のデータ セットの後に、異なる値を持つより類似したデータ セットが続きます。

direction 1、などのすべてのデータセットについて、このファイルから2番目と3番目の列を読み取りたいですdirection 2。現在、関数内で次のコード行を使用して、以下に示すようにデータを読み取ります。

fid = fopen(output_file); % open the output file

dotOUT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it as string ('%s') into one big array, row by row
dotOUT_fileContents = dotOUT_fileContents{1};
fclose(fid); %# close the file 

%# find rows containing 'SV' 
data_starts = strmatch('SV',...
    dotOUT_fileContents); % data_starts contains the line numbers wherever 'str2match' is found
nDataRows=data_starts(2)-data_starts(1)-1;
ndata = length(data_starts); % total no. of data values will be equal to the corresponding no. of 'str2match' read from the .out file

%# loop through the file and read the numeric data
for w = 1:ndata

    %# read lines containing numbers
    tmp_str = dotOUT_fileContents(data_starts(w)+1:data_starts(w)+nDataRows); 

    %# 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
    data_matrix_column_wise(:,w) = y; % convert the part of the character containing data into number

    %# assign output in terms of lag and variogram values 
    lag_column_wise(:,w)=data_matrix_column_wise(2:6:nLag*6-4,w);
    vgs_column_wise(:,w)=data_matrix_column_wise(3:6:nLag*6-3,w); 
end

上記の出力ファイルに星が表示されていない場合、この関数はうまく機能していました。ただし、上記のような出力ファイルの 1 つに星が含まれており、その場合、上記のコードは失敗しました。列 2 と列 3 を正しく読み取ることができるように、データ内のを処理するにはどうすればよいですか?

4

2 に答える 2

3

問題は、コードのこの部分です:

sscanf(z,'%f')

浮動小数点数との一致を強制していますが、星に遭遇すると失敗します。それを次のようなものに置き換えたほうがよいかもしれません

textscan(z, '%f %f %f %s %f %f', 'Delimiter', '\t')

cell2mat を削除し、次の行を適切に変更して、文字列の存在をテストします。

または、これはそれらの星の意味によって異なります。星をゼロまたは意味のあるものに置き換えるだけで、現在のコードは正常に機能します。

于 2012-07-12T17:54:45.127 に答える
1

次のように、すべての列を文字列として読み取ります。

C = textscan(fid, '%s%s%s%s%s%s');

これにより、6 列のセル配列である C が得られます。C の要素には、列 n と行 m に対して C{1,n}{m} としてアクセスできます。次に、 for ループで 48 から値を減算して、次のような数値を取得できます

for n=1:6
    for m=1:M
        A(m,n) = C{1,n}{m}-48;
    end
end

必要なものは行列 A に格納されます。もちろん、C{1,n}{m} をこれらの 8 つの星と比較して、それをどうしたいかを決めることができます。

于 2012-07-13T00:28:45.920 に答える