views:

270

answers:

4

I have a file a.txt:

03,17.406199
05,14.580129
07,13.904058
11,14.685388
15,14.062603
20,14.364573
25,18.035175
30,21.681789
50,22.662820

The number of rows in the file are not known. I want to read the file and store

3
5
7
11
15
20
30
50

in one array and the float values in another.

How do I read in a file when the length of data is not known?

+2  A: 

Read line-by-line until you hit the EOF marker.

John at CashCommons
A: 

Certain functions (like TEXTSCAN) will continue recycling the format string until the end of the file is reached. Other functions (like FSCANF) can take Inf as a size option, indicating that it should continue reading until the end of the file. If you are reading data line-by-line in a loop, you can use the FEOF function to test if the end of the file has been reached.

gnovice
+5  A: 
Dima
A: 

Since your elements are separated by commas, take a look at csvread. This should read the entire file into a single matrix, which you can then split into the two vectors you want.

Disclaimer: not tested!

fileContents = csvread('a.txt');
integerColumn = fileContents(:, 1);
doubleColumn = fileContents(:, 2);
Dan Vinton