将.txt数据读取到Matlab [英] Reading .txt data to Matlab
问题描述
我正在尝试从此.txt中读取数据:
I am trying to read data from this .txt:
obiekt.DEF
Timeplot
Column01: P abs h01 L1 [W]
Column02: P abs h01 L2 [W]
Column03: P abs h01 L3 [W]
Column04: P abs h01 Sum [W]
Time Column01 Column02 Column03 Column04
11.03.2004 09:17:02 23500 19812 21529 64,84e+3
11.03.2004 09:17:05 23316 19789 21519 64,62e+3
11.03.2004 09:17:08 23207 19759 21392 64,36e+3
我只需要来自列01、02、03的数据.有些数据带有,"而不是.".怎么改变呢?我有很多这样的文件.我尝试了此功能,但是它将所有数据写入一个变量.
I only need data from column: 01,02,03. Some data have ',' instead '.'. How to change it? I have many file like this. I tried this function, but it write all data to one variable.
b=textread('test.txt','%s','delimiter',' ','whitespace',' ');
推荐答案
您可以使用textscan:
You could use textscan:
filename='myfile.txt';
fid=fopen(filename,'r');
data=textscan(fid,'%*s%*s%s%s%s%*s','HeaderLines',10,'CollectOutput',1);
fclose(fid);
data=strrep(data{1},',','.');
data=cellfun(@str2num, data);
设置HeaderLines
设置要忽略的前10行.设置CollectOutput
将相同类型的项目分组到一个单元格数组中(因此我们得到3列字符串). formatspec '%*s%*s%s%s%s%*s'
忽略日期,时间和Column04,并将Column01-03转换为字符串.然后strrep
用句点替换逗号. cellfun
在每个单元格上调用str2num
并将字符串转换为数字.
Setting HeaderLines
sets the first 10 lines to be ignored. Setting CollectOutput
groups items of the same type into a cell array (so we get 3 columns of strings). The formatspec '%*s%*s%s%s%s%*s'
ignores the date, time and Column04 and converts Column01-03 to strings. Then strrep
replaces the commas with periods. cellfun
calls str2num
on each cell and converts the string to a number.
这篇关于将.txt数据读取到Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!