Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何从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 到现在为止,我一直按照以下方式拆分这些数据并从每个文件中读取变量(time and name_i) :

[TIME(:,j), name_i(:,j)]=textread('filename','%f\t%f','headerlines',5); 但是现在我将这些文件的数据生成为1个文件,如开头所示。例如,我想分别将name_3,name_6,_9以及其他类似的数据读取并存储在向量TIME1,TIME2,TIME3,TIME4,TIME5中。



回答:

首先,我建议您不要使用诸如TIME1,TIME2之类的变量名,因为那样会很快变得混乱。相反,您可以例如使用具有五行(每孔一个)和一或两列的单元阵列。在下面的示例代码中, wellData{2,1}是第二口井的时间, wellData{2,2}是相应的油价SC-年。

可能会有更优雅的阅读方式。快速操作:

%# 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

更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 01:06


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.