TEXTSCANを使用してファイルの内容を文字列のセル配列に読み取り、 CELLFUNおよびSTR2NUMを使用して文字列を数値に変換し(のような文字'a'
で'b'
、空の行列になります[]
)、空のセルがあるセル配列の行を削除できます。それらの中で、 CELL2MATを使用して残りのデータをN行2列の行列に変換します。
fid = fopen('junk.txt','r'); %# Open the file
data = textscan(fid,'%s %s','CollectOutput',true); %# Read the data as strings
fclose(fid); %# Close the file
data = cellfun(@str2num,data{1},'UniformOutput',false); %# Convert to numbers
data(any(cellfun('isempty',data),2),:) = []; %# Remove empty cells
data = cell2mat(data); %# Convert to N-by-2 array
質問のサンプルファイルを考えると、マトリックスは次のdata
ようになります。
>> data
data =
12 1
21 2
32 7
22 4
13 5
31 6
そして、次のように2番目の列で5より大きい値を持つ行を取得できます。
>> data(data(:,2) > 5,:)
ans =
32 7
31 6