フィルターのクリア

Read file and find maximum value

1 回表示 (過去 30 日間)
Konstantinos Belivanis
Konstantinos Belivanis 2015 年 11 月 4 日
編集済み: per isakson 2015 年 11 月 5 日
Hello all,
I have files that contain numeric and text values. What I want is to read them and find the maximum value of the second column. I have attached a file for better understanding. What I have tried so far is:
files = dir('*.txt');
i=1;
for file = 'files'
csv = dlmread(file.name,'');
end
But I am getting the error "Mismatch between file and format string. Trouble reading 'Numeric' field from file"
Thank you in advance.
  1 件のコメント
dpb
dpb 2015 年 11 月 5 日
Is this one file or a whole bunch of files concatenated together?
Either way, per
>> help dlmread
dlmread Read ASCII delimited file.
...
All data in the input file must be numeric. dlmread does not operate
on files containing nonnumeric data, even if the specified rows and
columns for the read contain numeric data only.
The simplest way to read depends on the answer to the above question, however...

サインインしてコメントする。

回答 (1 件)

per isakson
per isakson 2015 年 11 月 5 日
編集済み: per isakson 2015 年 11 月 5 日
The file, 11a-LEP.txt, contains multiple blocks. The blocks in turn consists of a header and two columns of numerical data. The first row of each data block consists of two zeros, &nbsp 0 0.
The function, cssm, concatenates the numerical data vertically. Try
>> [ num, buf ] = cssm( '11a-LEP.txt' );
>> whos
Name Size Bytes Class Attributes
buf 39x1 11232 cell
num 429x2 6864 double
>>
where
function [ num, buf ] = cssm( filespec )
str = fileread( filespec );
% cac = regexp( str, '(?<=\s+X +LEP\s+).+?(?=(\s+X +LEP\s+)|$)', 'match' );
cac = regexp( str, '(?<=LEP\s+).+?(?=(\s+X)|$)', 'match' );
%
buf = cell( length( cac ), 1 );
for jj = 1 : length( cac ) % loop over all blocks
buf(jj) = textscan( cac{jj}, '%f%f', 'CollectOutput',true );
end
num = cell2mat( buf );
end
Note: &nbsp The code contains to alternate calls of regexp. The first, which is commented out, is significantly slower. Another fast alternative is
cac = regexp( str, '\s+X +LEP\s+', 'split' ); cac(1)=[];
If it's okay to concatenate the blocks there is a simple solution:
fid = fopen('11a-LEP.txt');
cac = textscan( fid, '%f%f', 'CollectOutput',true, 'CommentStyle','X' );
fclose( fid );
&nbsp
"maximum value of the second column" &nbsp Block-wise or over all blocks? And what about the zero, which is the maximum value?

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by