如何从Matlab中的记事本文件块读取数据? [英] How to read data in chunks from notepad file in Matlab?

查看:458
本文介绍了如何从Matlab中的记事本文件块读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据格式如下:

  TABLE NUMBER 1 
FILE:name_1
name_2
TIME name_3
day name_4
-0.01 0
364.99 35368.4
729.99 29307
1094.99 27309.5
1460.99 26058.8
1825.99 25100.4
2190.99 24364
2555.99 23757.1
2921.99 23240.8
3286.99 22785
3651.99 22376.8
4016.99 22006.1
4382.99 21664.7
4747.99 21348.3
5112.99 21052.5
5477.99 20774.1
5843.99 20509.9
6208.99 20259.7
6573.99 20021.3
6938.99 19793.5
7304.99 19576.6
TABLE NUMBER 2
FILE :name_1
name_5
TIME name_6
day name_7
-0.01 0
364.99 43110.4
729.99 37974.1
1094.99 36175.9
1460.99 34957.9
1825.99 34036.3
2190.99 33293.3
2555.99 32665.8
2921.99 32118.7
3286.99 31626.4
3651.99 31175.1
4016.99 30758
4382.99 30368.5
4747.99 30005.1
5112.99 29663
5477.99 29340
5843.99 29035.2
6208.99 28752.4
6573.99 28489.7
6938.99 28244.2
7304.99 28012.9
TABLE NUMBER 3

到目前为止,我正在分解这些数据并读取变量(时间和名字_i)从每个文件以下列方式:

  [TIME(:,j ),name_i(:,j)] = textread('filename','%f\t%f','headerlines',5); 

但是现在我正在将这些文件的数据生成为一个文件,如开头所示。例如,我想分别读取和存储TIME1,TIME2,TIME3,TIME4,TIME5中的TIME数据,分别用于name_3,name_6,_9,并且类似地用于其他。

解决方案首先,我建议你不要使用诸如TIME1,TIME2等变量名,因为它会很快变得混乱。相反,你可以使用5行(每个井一个)和一个或两个列的单元格阵列。在下面的示例代码中, wellData {2,1} 是第二口井的时间, wellData {2,2} 是相应的Oil Rate SC - Yearly。

可能有更优雅的阅读方式;这里有一些快速的:

$ $ p $ $ $ $ $打开文件
fid = fopen('Reportq.rwo');

%#将它读入一个大数组中,逐行
fileContents = textscan(fid,'%s','Delimiter','\\\
');
fileContents = fileContents {1};
fclose(fid); %#不要忘记再次关闭文件

%#查找包含TABLE NUMBER
wellStarts = strmatch('TABLE NUMBER',fileContents)的行。
nWells =长度(wellStarts);

%#循环读取数值数据
wellData = cell(nWells,2);
wellStarts = [wellStarts; length(fileContents)];

for w = 1:nWells
%#读取包含数字的行
tmp = fileContents(wellStarts(w)+5:wellStarts(w + 1)-1);
%#将字符串转换为数字
tmp = cellfun(@ str2num,tmp,'uniformOutput',false);
%#catenate array
tmp = cat(1,tmp {:});
%#assign output
wellData(w,:) = mat2cell(tmp,size(tmp,1),[1,1]);
end


My data is in following format:

TABLE NUMBER 1
                                                FILE: name_1
                                                            name_2
TIME    name_3  
day name_4  
-0.01   0   
364.99  35368.4 
729.99  29307   
1094.99 27309.5 
1460.99 26058.8 
1825.99 25100.4 
2190.99 24364   
2555.99 23757.1 
2921.99 23240.8 
3286.99 22785   
3651.99 22376.8 
4016.99 22006.1 
4382.99 21664.7 
4747.99 21348.3 
5112.99 21052.5 
5477.99 20774.1 
5843.99 20509.9 
6208.99 20259.7 
6573.99 20021.3 
6938.99 19793.5 
7304.99 19576.6 
TABLE NUMBER 2
                                                FILE: name_1
                                                            name_5
TIME    name_6  
day name_7  
-0.01   0   
364.99  43110.4 
729.99  37974.1 
1094.99 36175.9 
1460.99 34957.9 
1825.99 34036.3 
2190.99 33293.3 
2555.99 32665.8 
2921.99 32118.7 
3286.99 31626.4 
3651.99 31175.1 
4016.99 30758   
4382.99 30368.5 
4747.99 30005.1 
5112.99 29663   
5477.99 29340   
5843.99 29035.2 
6208.99 28752.4 
6573.99 28489.7 
6938.99 28244.2 
7304.99 28012.9 
TABLE NUMBER 3

Till now I was splitting this data and reading the variables (time and name_i) from each file in following way:

[TIME(:,j), name_i(:,j)]=textread('filename','%f\t%f','headerlines',5);

But now I am producing the data of those files into 1 file as shown in beginning. For example I want to read and store TIME data in vectors TIME1, TIME2, TIME3, TIME4, TIME5 for name_3, name_6, _9 respectively, and similarly for others.

解决方案

First of all, I suggest you don't use variable names such as TIME1,TIME2 etc, since that gets messy quickly. Instead, you can e.g. use a cell array with five rows (one for each well), and one or two columns. In the sample code below, wellData{2,1} is the time for the second well, wellData{2,2} is the corresponding Oil Rate SC - Yearly.

There might be more elegant ways to do the reading; here's something quick:

%# open the file
fid = fopen('Reportq.rwo');

%# read it into one big array, row by row
fileContents = textscan(fid,'%s','Delimiter','\n');
fileContents = fileContents{1};
fclose(fid); %# don't forget to close the file again

%# find rows containing TABLE NUMBER
wellStarts = strmatch('TABLE NUMBER',fileContents);
nWells = length(wellStarts);

%# loop through the wells and read the numeric data
wellData = cell(nWells,2);
wellStarts = [wellStarts;length(fileContents)];

for w = 1:nWells 
    %# read lines containing numbers
    tmp = fileContents(wellStarts(w)+5:wellStarts(w+1)-1);
    %# convert strings to numbers
    tmp = cellfun(@str2num,tmp,'uniformOutput',false);
    %# catenate array
    tmp = cat(1,tmp{:});
    %# assign output
    wellData(w,:) = mat2cell(tmp,size(tmp,1),[1,1]);
end

这篇关于如何从Matlab中的记事本文件块读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆