How do I extract sections of data from a csv file?
Show older comments
I am having difficulty extracting the data I require from a csv file. I have been provided with a csv file which has the outputs of a number of simulations. However I have hit a dead-end on how to extract the data without doing it individually, i.e. opening in the import window and selecting the range of cells I wish to import by altering the range selected and doing this for each simulation. The format of the csv is attached, the output comes in the form of a table where one simulation is produced with the given headings along the top and the simulated results in the columns below. The next simulation is then produced below in the same form. Ideally I would want to input all the data directly into vectors, e.g. the year data into an 11x10 vector, with each column holding the relevant data for each simulation, i.e the first column of the vector holding A3:A13, the second column holding A17:A27 and so on. Any advice on how to extract the data would be greatly appreciated. Thanks in advance.
3 Comments
I cannot guess if the pile of commas have a meaning and in the number of terms in this line has any meaning: "
END,END,END,END,END,END,END,END,END,END,END,END,END,END
Perhaps the order of characters in this line can change or is not important:
Year,Q ,I ,W ,J ,Y ,DM ,K ,D ,P ,CM ,C ,B ,R
There are too many details which must be guessed such that posting a matching answer is very unlikely based on the currently provided information. What exactly does this mean: "without doing it individually"?
Please define the underlying pattern of the file accurately. Why do you assume, that the "Year" can be extracted as a 10x10 matrix - when you have 10 "measurements" with 11 lines?
Andrew Hair
on 18 Jun 2015
dpb
on 18 Jun 2015
See Answer below--there's really nothing to worry about regarding the format/interpretation as far as I can see--looks like a machine-generated csv file with a blank line after the last set of values for each year and the explicit commas for each field irrespective of data in the field or not for the title line.
There is something a little funky as is often the case in me experience using textscan--I had to insert an extra fgetl to get the file pointer to the next line after the initial section read; trying the loop w/o got off somehow...
Accepted Answer
More Answers (0)
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!