Import text file with blank lines. Matlab not replacing them with NaN
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Matlab is not replacing my blank lines in my txt file with NaN but just joins all the data together. Unfortunately I need to data in the exact order it is as each line is a unique timestamp but the times are do not come in the txt file.
Any ideas? Tried importdata and textscan with no luck. Using R2014b
採用された回答
per isakson
2015 年 1 月 29 日
編集済み: per isakson
2015 年 1 月 30 日
Remains (at least) two possibilities
- a loop over fgetl
- read the file as one string, replace empty lines by 'nan nan ... ' and parse with textscan
Example (R2013a)
>> cac = cssm;
>> cac{:}
ans =
16 2 3 13
5 11 10 8
NaN NaN NaN NaN
9 7 6 12
4 14 15 1
where
function cac = cssm
str = fileread('cssm.txt');
str = regexprep( str, '(?<=\r?\n)[ ]*(?=\r?\n)', 'nan nan nan nan');
cac = textscan( str, '%f%f%f%f', 'CollectOutput', true );
end
and where cssm.txt contains
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
 
Replace
str = regexprep( str, '(?<=\r?\n)[ ]*(?=\r?\n)', 'nan nan nan nan');
by
str = regexprep( str, '(?<=\r?\n)[ ]*(?=\r?\n)' ...
, 'nan nan nan nan', 'emptymatch' );
to handle empty lines
10 件のコメント
Requires at least one char(32) in "empty" lines
Hi Per,
I would replace your pattern by
'(?<=(\r?\n|^))\s*(?=(\r?\n|$))'
to capture potential tabs or spaces on empty lines as well as cases where the file starts or ends with an empty line.
per isakson
2015 年 1 月 29 日
編集済み: per isakson
2015 年 1 月 29 日
Hi Cedric,
Regular expressions require thorough testing. You propose three modifications
- add ^| to handle leading empty lines. Yes, I agree.
- replace [ ]* by \s* to handle potential delimiter on empty lines. OK, but there is one problem, \s* matches new-line. Replacing by \s*? (lazy) solves that.
- add $| to handle trailing empty lines. No, because one occurrence of new-line at the end of the file does not indicate an empty line. Interactively created files may or may not have new-line at the end of the last line. Automatically created files "always" have new-line at the end of the last line.
My new expression is
'(?<=\r?\n|^)\s*?(?=\r?\n)'
I agree about point 3, but for point 2: \s* should not match the new-line when it is matched by the look forward. In that case, the pattern is matched because the 0 occurrence defined by the * is verified.
>> regexprep( 'ab', '(?<=a)b*(?=b)', 'z', 'emptymatch' )
ans =
azb
per isakson
2015 年 1 月 29 日
編集済み: per isakson
2015 年 1 月 29 日
Hi Cedric,
To illustrate how I think, I have created three text files, cssm_0.txt, cssm_1.txt, cssm_2.txt, with zero, one and two empty lines at the end, respectively. The image are clips of the files in NotePad++.

 
With the expression
'(?<=\r?\n|^)\s*?(?=\r?\n)'
I get the results below
>> clear all,cac = cssm('cssm_0.txt');cac{:}
ans =
NaN NaN NaN NaN
16 2 3 13
5 11 10 8
NaN NaN NaN NaN
NaN NaN NaN NaN
9 7 6 12
4 14 15 1
>> clear all,cac = cssm('cssm_1.txt');cac{:}
ans =
NaN NaN NaN NaN
16 2 3 13
5 11 10 8
NaN NaN NaN NaN
NaN NaN NaN NaN
9 7 6 12
4 14 15 1
NaN NaN NaN NaN
>> clear all,cac = cssm('cssm_2.txt');cac{:}
ans =
NaN NaN NaN NaN
16 2 3 13
5 11 10 8
NaN NaN NaN NaN
NaN NaN NaN NaN
9 7 6 12
4 14 15 1
NaN NaN NaN NaN
NaN NaN NaN NaN
>>
\s* or \s*?
I can reproduce your example
>> regexprep( 'ab', '(?<=a)b*(?=b)', 'z', 'emptymatch' )
ans =
azb
and the lazy ? doesn't hurt
>> regexprep( 'ab', '(?<=a)b*?(?=b)', 'z', 'emptymatch' )
ans =
azb
However it doesn't work with the string from the text file. With the expression
'(?<=\r?\n|^)\s*(?=\r?\n)'
I get
>> clear all,cac = cssm('cssm_1.txt');cac{:}
ans =
NaN NaN NaN NaN
16 2 3 13
and with the expression
'(?<=\r\n|^)\s*(?=\r\n)'
I get
>> clear all, clear classes,cac = cssm('cssm_1.txt');cac{:}
ans =
NaN NaN NaN NaN
16 2 3 13
5 11 10 8
NaN NaN NaN NaN
NaN NaN NaN NaN
9 7 6 12
4 14 15 1
NaN NaN NaN NaN
The problem is with the "?" in \r?\n - I think. In this context "\s*" matches "\r" and the look-ahead is happy with "\n". With "\s*?" the "\r" goes to the look-ahead.
I used "\r*\n" in the first place to match both the DOS and the Windows style of new-line.
Thank you for the illustration, I will have a look in half an hour!
Hi Per,
Thank you for all the illustrations, I agree with all of your conclusions! After spending quite a bit of time working on alternate approaches based on tokens (which happens to be a little bit slower ultimately), I just realize that we don't need to match the eventual \r in the look behind.
Thanks a lot guys!!
Hi Cedric,
You are right, "\r" is not needed in the "look behind". And possibly, it saves on execution time to exclude it.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
