Read in a .txt format with textread

조회 수: 9 (최근 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 CenterFile Exchange에서 Workspace 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