how to read text file into formatted array

Hi
I have a text file which contains many rows of this shape
'0862;1;10/09/2002;09:15:59;23.845089;38.018470;486253.80;4207588.10'
I need to read each line in different cells. each cell contains data between ";" and the third and forth one are date and time while others are double numbers.
would you please help me to do so

2 Kommentare

per isakson
per isakson am 15 Dez. 2014
Bearbeitet: per isakson am 15 Dez. 2014
"I need to read each line in different cells" &nbsp Why is that? And what does it exactly mean?
H D
H D am 15 Dez. 2014
thanks for comment. I need to read the whole txt file in a matrix. I presented the first line as a sample. each cell data is separated using semicolon as mentioned. in this case third and forth are date and time of day and all the rest can be saved as double in a matrix.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

per isakson
per isakson am 15 Dez. 2014
Bearbeitet: per isakson am 15 Dez. 2014

1 Stimme

Here is a function, which does what I believe is what you want. The third column of out is a serial date number.
>> out = cssm('cssm.txt')
out =
1.0e+06 *
0.0009 0.0000 0.7315 0.0000 0.0000 0.4863 4.2076
0.0009 0.0000 0.7315 0.0000 0.0000 0.4863 4.2076
0.0009 0.0000 0.7315 0.0000 0.0000 0.4863 4.2076
>> datestr( out(:,3), 31 )
ans =
2002-10-09 09:15:59
2002-10-09 09:15:59
2002-10-09 09:15:59
where
function out = cssm( filespec )
fid = fopen( filespec );
cac = textscan( fid, '%f%f%s%s%f%f%f%f' ...
, 'Delimiter',';', 'CollectOutput',true );
fclose( fid );
str = cell2mat(cac{2});
sdn = datenum( str, 'mm/dd/yyyyHH:MM:SS');
out = cat( 2, cac{1}, sdn, cac{3} );
end
and where cssm.txt contains
0862;1;10/09/2002;09:15:59;23.845089;38.018470;486253.80;4207588.10
0862;1;10/09/2002;09:15:59;23.845089;38.018470;486253.80;4207588.10
0862;1;10/09/2002;09:15:59;23.845089;38.018470;486253.80;4207588.10

4 Kommentare

H D
H D am 15 Dez. 2014
what is cssm! matlab does not find it
H D
H D am 15 Dez. 2014
this is the error I get while cssm is not known by the version of matlab I use:
>> out = cssm('Truck.txt') Undefined function 'cssm' for input arguments of type 'char'.
per isakson
per isakson am 15 Dez. 2014
Bearbeitet: per isakson am 15 Dez. 2014
cssm is the function defined after the word, where, in my answer.
H D
H D am 15 Dez. 2014
:D sorry for my crazy question. you are right. It works perfectly. thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 15 Dez. 2014
Bearbeitet: Azzi Abdelmalek am 15 Dez. 2014

0 Stimmen

fid=fopen('FileName.txt')
tline = fgetl(fid)
out=[];
while ischar(tline)
out{end+1,1} =tline
tline= fgetl(fid);
end
fclose(fid)
%You can aadd
A=regexp(out,';','split')
n=numel((A{1}))
B=reshape([A{:}],n,[])'
res=[cell

Kategorien

Mehr zu Large Files and Big Data finden Sie in Hilfe-Center und File Exchange

Gefragt:

H D
am 15 Dez. 2014

Kommentiert:

H D
am 15 Dez. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by