0

ファイルのすべての行が配列内の異なる要素になるように、Matlab で .txt ファイルを解析する必要があります。配列の各要素も整数の配列になります。したがって、.txt ファイルから配列の配列を作成する必要があります。

私が抱えている問題は、ファイルを解析するためにどの関数を使用すればよいか分からないことです。importdata(filename) を使用すると、ファイルの最初の行のみが解析されます。textscan を使用すると、ファイルが列で解析され、ファイルは次のようにフォーマットされます。

1 1 1 1 1
13 13 13 13 13

2 2 2 2 2
14 14 14 14 14

各行を配列にして、データを比較するために使用できるようにする必要があります。

私の目的のために機能するこれらの機能のいずれかのオプションはありますか? MATLAB のドキュメントを調べてみましたが、意味がわかりません。

4

2 に答える 2

0

データが 5 つの数値の後に 1 つの数値が続く特定の形式になることがわかっている場合は、dlmread を使用して、結果の行列を書式設定できます。

data = dlmread('data.txt',' ');
multipleValueRows = data(1:2:end,:);
singleValueRows = data(2:2:end,1);

データ マトリックスのサイズは (ファイルの行数) x 5 列です。数値が 1 つしかない行では、データ マトリックスの列 2 ~ 5 にゼロが含まれます。

于 2013-03-18T17:44:28.963 に答える
0

各配列を異なるサイズにする必要がある場合は、cell 配列を使用してそれらを含める必要があります。このようなもの:

fid = fopen('test.txt'); %opens the file
tline = fgets(fid); % reads in the first line
data = {}; % creates an empty cell array
index = 1; % initializes index
while ischar(tline) % loops while the line that has just been read contains characters, that is, the end of the file has not been reached.  
    data{index} = str2num(tline); % converts the line that has just been read in to a string and assigns it to the current column of data
    tline = fgets(fid); % reads in the next line 
    index = index + 1; % increments index
end

fclose(fid);
于 2013-03-18T17:43:40.480 に答える