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

4 visualizaciones (últimos 30 días)
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
  4 comentarios
Guillaume
Guillaume el 16 de Oct. de 2015
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.

Iniciar sesión para comentar.

Respuesta aceptada

Kirby Fears
Kirby Fears el 16 de Oct. de 2015
Editada: Kirby Fears el 16 de Oct. de 2015
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]');
fclose(fid);
% Split across delim1
myData = cellfun(@(c)strsplit(c,delim1,'CollapseDelimiters',false)',...
myData{1},'UniformOutput',false);
% Loop over each row to split across delim2 within delim1
for row = 1:numel(myData),
myData{row}=cellfun(@(c)strsplit(c,delim2,'CollapseDelimiters',false),...
myData{row},'UniformOutput',false);
% Convert from char to double
for col = 1:numel(myData{row}),
myData{row}{col} = str2double(myData{row}{col});
end
end
The result is:
myData =
{3x1 cell}
{3x1 cell}
myData{1} =
[1x28 double]
[1x19 double]
[1x3 double]
Hope this helps.
  2 comentarios
Kelly Kyriakou
Kelly Kyriakou el 17 de Oct. de 2015
Editada: Kelly Kyriakou el 17 de Oct. de 2015
This really helps a lot!!! How can I manage these data to be stored in one variable?
Kirby Fears
Kirby Fears el 19 de Oct. de 2015
Editada: Kirby Fears el 19 de Oct. de 2015
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by