Read in a .txt format with textread

6 ビュー (過去 30 日間)
Iris Lichtblau
Iris Lichtblau 2016 年 3 月 8 日
回答済み: dpb 2016 年 3 月 8 日
Hi,
I want to read in a txt file with textread
File 20130419.txt:
19.04.2013;00:00:00;0.000;0;-9.999;0;13;0.08;10640;<SPECTRUM>ZERO</SPECTRUM>
19.04.2013;00:01:00;0.000;0;-9.999;0;13;0.08;10698;<SPECTRUM>ZERO</SPECTRUM>
19.04.2013;00:02:00;0.000;0;-9.999;0;13;0.08;10660;<SPECTRUM>ZERO</SPECTRUM>
...
i wrote in Matlab:
[date,time,R,NS,Z,RT,T,heat,laser,spec] = textread('20130419.txt', ...
'%f %f %f %d %f %d %f %f %f %s', ...
'delimiter',';', ...
-1, ...
'headerlines',0);
now matlab told me this:
Error using dataread
Param/value pairs must come in pairs.
Error in textread (line 174)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
Error in bspBA (line 2)
[date,time,R,NS,Z,RT,T,heat,laser,spec] = textread('20130419.txt','%f %f %f %d %f %d %f %f %f
%s','delimiter',';',-1,'headerlines',0);
How can I read in my file?
  2 件のコメント
Star Strider
Star Strider 2016 年 3 月 8 日
Where does the ‘-1’ come from here:
... ,'delimiter',';',-1, ...
What do you want to do with it?
dpb
dpb 2016 年 3 月 8 日
I presume it was intended to be the repetition counter for the format string use; <0 is same as inf but it was misplaced in the argument list order--must follow the format string directly.

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

回答 (1 件)

dpb
dpb 2016 年 3 月 8 日
Besides the comment noted above by Star, your format string doesn't match the record -- the date/time fields aren't valid '%f' fields; you'll either have to read them as character or parse the day.month.year and hr:min:sec fields as separate variables.
textread has much to commend it for simple files where can avoid returning a cell array and the hoopla of fopen/fclose but it doesn't handle such fields as date/times as well as some of the other alternatives--*textscan* in particular which with a recent Matlab release has the datetime class and a date format string to read the dates and times directly. But, to read this file, the format string would need to be
fmt=['%2d.%2d.%2d %2d:%2d:%2d' repmat('%f',1,6) '%s'];
[day,mon,yr,hr,min,sec,R,NS,Z,RT,T,heat,laser,spec] = textread('20130419.txt', fmt, inf, ...
'headerlines',0, ...
'delimiter',';');
All in all, it would be better to move to textscan, use the %D format string and return the numeric data in an array rather than create so many variables.

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by