MATLAB Answers


How to read a csv which contains both string and numbers with diferrent number of delimiters at each row?

Asked by Kelly Kyriakou on 16 Oct 2015
Latest activity Edited by Kirby Fears on 19 Oct 2015
I have a csv file which contains both string and numbers so I use textscan to read it. My problem is that each row has diferrent number of delimiters as a result different number of columns. How I can read it?
thank you very much


Show 1 older comment
10022,10021,h,600,660,398,1700.0000,0.1171,0.0000,62.0000,61.9851,100,A,0,0.0000,398,0.0000,0.0000,3980.0000,0.0000,0.0000,0.0000,0.0000,0.0000,398,0,0,0,"<LineString><coordinates>21.803638,40.354573,0.0 21.802329,40.357851,0.0 21.801662,40.361182,0.0 21.801567,40.368701,0.0 21.801377,40.374507,0.0 21.799473,40.378029,0.0 21.791859,40.389260,0.0 21.789004,40.391639,0.0 21.784717,40.393051,0.0</coordinates></LineString>",10022,10021,
10022,10003,h,600,660,326,1700.0000,0.0959,0.0000,62.0000,61.9407,100,A,0,0.0000,326,0.0000,0.0000,3260.0000,0.0000,0.0000,0.0000,0.0000,0.0000,326,0,0,0,"<LineString><coordinates>21.802059,40.353897,0.0 21.803510,40.349846,0.0 21.804169,40.348174,0.0</coordinates></LineString>",10022,10003,
This looks more like an xml file or it is some sort of custom formatting. Attach an entire file to know for sure.
In any case, it's not a file that can be read with matlab's csv file functions. You're going to have to write your own parser.
This is the file and it is created automatically from another software

Sign in to comment.

1 Answer

Answer by Kirby Fears on 16 Oct 2015
Edited by Kirby Fears on 16 Oct 2015
 Accepted Answer

Hi Kelly,
Guillame is right that this data is probably some version of xml that should be stored as an xml file and read using xmlread() in Matlab. This should be your first line of investigation. Is your data being converted from xml format to csv format at any point? If so, can you just get the raw xml instead?
However, I wrote a parser for the csv version anyway. I dropped your two lines of example data into 'kellydata.csv' and used the following code to generate a cell containing each line as a triplet of double arrays. The arrays are (1) before the LineString block, (2) in the LineString block, and (3) after the LineString block.
delim1 = {',"<LineString><coordinates>','</coordinates></LineString>",'};
delim2 = {','};
% Read each line as a single string.
fid = fopen('kellydata.csv');
myData = textscan(fid,'%[^\n]');
% Split across delim1
myData = cellfun(@(c)strsplit(c,delim1,'CollapseDelimiters',false)',...
% Loop over each row to split across delim2 within delim1
for row = 1:numel(myData),
% Convert from char to double
for col = 1:numel(myData{row}),
myData{row}{col} = str2double(myData{row}{col});
The result is:
myData =
{3x1 cell}
{3x1 cell}
myData{1} =
[1x28 double]
[1x19 double]
[1x3 double]
Hope this helps.


This really helps a lot!!! How can I manage these data to be stored in one variable?
The data is stored in one variable already. I can give more help if you know exactly how you want the data stored or how you want it retrieved. You could organize it a bit differently inside the cell array or use a different data structure if necessary.

Sign in to comment.