Read in a .txt format with textread
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Star Strider
le 8 Mar 2016
Where does the ‘-1’ come from here:
... ,'delimiter',';',-1, ...
What do you want to do with it?
dpb
le 8 Mar 2016
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.
Réponses (1)
dpb
le 8 Mar 2016
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.
0 commentaires
Voir également
Catégories
En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!