MATLAB Answers


how to extract data from a range of (slightly) varying csv files

Asked by Gerard Kluivers on 18 May 2018
Latest activity Commented on by Guillaume
on 21 May 2018
Hi there,
I'm trying to write a function that will import and plot relevant data from a csv file. This is because for every file I have, there is some settings above the data that vary in length. So for example; sometimes the data starts at row 20 and sometimes 50 . Also the length of the data column varies. The columns from which I want to extract data is always the same.
How can matlab recognise from what point to take the data?
I think this should be possible as the settings only take 2/3 columns and the data is 5 or 6 columns wide, so you can 'see' where the data starts if you let matlab 'search' in the 6th column if that is possible.
Thank you in advance!


@Gerard Kluivers: contrary to the misleading file extensions, those files are not CSV formatted at all: they are really text files containing XML formatted data. Probably your best bet would be to import them using some XML importing function. You could try MATLAB's inbuilt xmlread:
or one of the third-party functions on FEX:
Hello Gerard,
from observing all the files you provided it looks like all your required data is between the < segment1 > and < /segment1 > of the text file. is it the case with all the files or just the 3 you provided.
if its the same with all the files you can use these strings as "milestones" to identify the data and then extract them.
Stephen is right that the files are absolutely not csv. The extensions are very misleading.
However, while structurally similar to XML, the files are also not valid XML and I suspect that an XML parser will choke on them (e.g. lines like Limits2,Direction=< will wreak havoc with the XML parsing)

Sign in to comment.





0 Answers