Way of conserving memory when extracting data from CSV
이전 댓글 표시
Hi everybody I have few questions. I have some HUGE CSV files which I need in Matlab for analysis. The CSV it self has 5 columns. The columns of relevance are:
Column 1 is our date starting from early 2007 all the way till till mid 2011 in the form of mm/dd/yyyy.
Column 3 is our respective prices
Column 5 is the number of trades.
The questions I have are these:
1) How can I extract these 3 columns into a Matrix in MATLAB without taking too much memory (bear in mind that some of these CSV files have around 60 million rows)? Is there a way to decrease the memory of each cell Matlab allocates for the matrix? Please help with code.
2) How can I extract all the information into a non-string matrix (for analysis) for a specific year....ie only for 2009. So I would require to store in Matrix all information for 2009 (bearing in mind the memory limitations in 1).
Thanks so much.
댓글 수: 13
Matt Kindig
2013년 4월 12일
If memory is an issue, you might want to parse the file line-by-line (using fgetl() or similar). That way, you don't need to read the entire file into memory at once. Also, by retrieving one line at a time, you can easily implement your year=2009 inclusion criterion.
per isakson
2013년 4월 12일
Please provide a sample. Type a few lines in the Matlab editor.
per isakson
2013년 4월 12일
I cannot see any reason why I should guess and create a sample cvs-file!
per isakson
2013년 4월 12일
편집: per isakson
2013년 4월 12일
Does the file look like this?
04/29/2008,38:52.0,71.35,CTN08,2
04/29/2008,38:53.0,71.35,CTN08,2
04/29/2008,38:56.0,71.35,CTN08,3
04/29/2008,38:56.0,71.35,CTN08,1
04/29/2008,38:56.0,71.35,CTN08,1
04/29/2008,38:57.0,71.35,CTN08,1
Mate 2u
2013년 4월 12일
per isakson
2013년 4월 12일
What are the maximum and minimum values of column 3 and 5, respectively?
Mate 2u
2013년 4월 13일
per isakson
2013년 4월 13일
편집: per isakson
2013년 4월 13일
See the answer of ImageAnalyst. It is wasteful to store the numbers in double float (8 byte).
Maybe, volume can be stored in uint8.
>> intmax('uint8')
ans =
255
Image Analyst
2013년 4월 13일
The maximum determines the smallest data class you can use. See code in my answer.
per isakson
2013년 4월 13일
And price will never exceed
>> intmax('uint32')
ans =
4294967295
cents ????
채택된 답변
추가 답변 (1개)
Image Analyst
2013년 4월 12일
1 개 추천
What are the classes of each column? Are they all 8 byte (64 bit) doubles? For example, the number of trades might be able to be a 4 byte integer, and most of the floating point numbers could probably be single instead of double. By retrieving it a line at a time and using sscanf() you can place each value into the smallest type of variable that is appropriate for that number. For example, assuming no stock price is over $655.35 you could read in the number and multiply by 100 so that all stock prices are in cents rather than dollars. That way you can use 16 bit unsigned integer instead of a 32 bit single.
I don't have the toolboxes, but perhaps the Financial Toolbox or the Fixed Point Designer may have efficient ways of handling numbers like prices of stocks.
Like Matt said, perhaps you don't need all 60 million rows in memory at once - hopefully you can process it in chunks.
댓글 수: 4
Image Analyst
2013년 4월 13일
realmax('double')
realmax('single')
intmax('int32')
intmax('int16')
intmax('uint16')
intmax('uint8')
ans =
1.79769313486232e+308
ans =
3.402823e+38
ans =
2147483647
ans =
32767
ans =
65535
ans =
255
Image Analyst
2013년 4월 13일
Regarding your comment above, They could both be uint16 then. That's 2 bytes instead of 8, so that saves you a lot - a factor of 4 in memory for those two columns.
Mate 2u
2013년 4월 13일
Image Analyst
2013년 4월 13일
For example, maybe someone asks about 2010 prices, so you scan the file line by line, throwing away data if it belongs to any other year than 2010. Only if the year is 2010 do you use put it into your array. Other years just go into single variables because you used sscanf but you re-use (overwrite) those variables. So on a line by line basis you will have variables thisPrice, thisDay, thisVolume, thisYear, and only when this year = 2010 do you add thisPrice, thisDay, thisVolume to priceArray, dayArray, volumeArray.
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!