How can I read a specific txt column?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi Guys,
I'm sorry about the title, might be a little misleading, but I don't know what else to write. So, let me get into it, I'm really desperate about this.
So, I have a .his file (can be read as .txt) and it's tab-separated (TSV). It looks something like this:
History date Number of measurements Channel number Type of probe
24.03.10 16:49 2316 0 10
Type of history Channel 1 Channel 2
1 FH 40 G Ext. 1 FH 40 G Ext. 2
Date Value 1 Status 1 Unit 1 FH40 probe type 1
40260,1250000000 1,41551E-01 0 uSv/h 4
40260,1256944444 1,52440E-01 0 uSv/h 4
40260,1263888889 1,41551E-01 0 uSv/h 4
(etc etc)
As you can see, even though it's TSV, the txt formatting is all messed up. What I want to do is to read this file and get the values called "Value 1" (should be the 2nd column). I want to do their mean and get its highest value.
Thing is, I can't seem to do it and I'm going crazy!
2 件のコメント
Stephen23
2017 年 9 月 13 日
You have a bigger problem than tab separation: commas for the decimal radix. Do a search of this forum to learn how to deal with that.
dpb
2017 年 9 月 13 日
As far as just reading the data (ignoring the comma vis a vis period for decimal point issue Stephen points out altho I thought by now Matlab did use the local definitions for the installation as set by the OS?) just skip the (six by my count) header lines and read the data; you're interested in the second column and that the headers don't necessarily line up is immaterial; just ignore them.
回答 (2 件)
Akira Agata
2017 年 9 月 14 日
You can extract 2nd column and convert it to numeric array by the following code. After doing this process, you can obtain mean and max value of the 2nd column by mean(data) and max(data).
fileID = fopen('yourFile.his','r');
data = textscan(fileID, '%s%s%s%s%s',...
'Delimiter', '\t', 'TextType', 'string',...
'HeaderLines', 5); % Skip the first 5 lines
fclose(fileID);
% Extract 2nd column
data = data(2);
% Convert to double
data = str2double(strrep(data{1},',','.'));
0 件のコメント
Cedric
2017 年 9 月 14 日
Quickly my 2 cents as I just have 30s. If these commas are really an issue, replace them before parsing with usual functions.
content = sprintf( 'Header\n4,5\t8\tHello\n5,6\t3\tWorld\n' ) ;
%content = fileread( 'MyFile.his' ) ; % In your case.
content(content == ',') = '.' ;
parsed = textscan( content, '%f%d%s', 'HeaderLines', 1, 'Delimiter', '\t' ) ;
With this:
>> parsed
parsed =
1×3 cell array
[2×1 double] [2×1 int32] {2×1 cell}
>> parsed{1}
ans =
4.5000
5.6000
>> parsed{2}
ans =
2×1 int32 column vector
8
3
>> parsed{3}
ans =
2×1 cell array
'Hello'
'World'
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で MATLAB Report Generator についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!