Data extraction from a text file.

I have the following data:
74
: 69
: 65
: 50
: 39
: 28
: 19
: 8
I created the following code to read the data:
fid=fopen('kirby1.txt');
data=textscan(fid,'%f','delimiter',':');
but it turns the ":" into "NAN". Is there a way to read the data without the ":"?
Desire output:
74
69
65
50
39
28
19
8
Thanks for your help.

1 Kommentar

per isakson
per isakson am 20 Jul. 2012
Bearbeitet: per isakson am 20 Jul. 2012
Does the first line differ from the others?
I guess the NaNs are the non-excisting values before ":".

Melden Sie sich an, um zu kommentieren.

Antworten (3)

per isakson
per isakson am 20 Jul. 2012
Bearbeitet: per isakson am 20 Jul. 2012

0 Stimmen

If all lines have the same format try
data=textscan(fid,':%f');
--- Cont. ---
This work with R2012a
fid = fopen( 'cssm.txt' );
cac = textscan( fid, ':%f' )
where cssm.txt contains
: 74
: 69
: 65
...
--- Cont. ---
Or if the first line differs
function cssm()
fid = fopen( 'cssm.txt' );
ca1 = textscan( fid, '%f', 1 )
ca2 = textscan( fid, ':%f' )
end
where cssm.txt contains
74
: 69
: 65
...
Kirby Little
Kirby Little am 20 Jul. 2012

0 Stimmen

I got the following error:
??? Error using ==> textscan Badly formed format string.

2 Kommentare

per isakson
per isakson am 20 Jul. 2012
Bearbeitet: per isakson am 20 Jul. 2012
It is difficult for me to guess the reason to the error you see!
You didn't answer my question in my comment on your question.
Walter Roberson
Walter Roberson am 20 Jul. 2012
Kirby, please show your line of code for the textscan()

Melden Sie sich an, um zu kommentieren.

Andrei Bobrov
Andrei Bobrov am 20 Jul. 2012

0 Stimmen

f = fopen('yourtextfile.txt');
c = textscan(f,'%s');
fclose(f);
out = str2double(c{:}(1:2:end));

Kategorien

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

Produkte

Gefragt:

am 20 Jul. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by