Effacer les filtres
Effacer les filtres

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

7 vues (au cours des 30 derniers jours)
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 commentaires
Guillaume
Guillaume le 16 Oct 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.
Kelly Kyriakou
Kelly Kyriakou le 16 Oct 2015
This is the file and it is created automatically from another software

Connectez-vous pour commenter.

Réponse acceptée

Kirby Fears
Kirby Fears le 16 Oct 2015
Modifié(e) : Kirby Fears le 16 Oct 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 commentaires
Kelly Kyriakou
Kelly Kyriakou le 17 Oct 2015
Modifié(e) : Kelly Kyriakou le 17 Oct 2015
This really helps a lot!!! How can I manage these data to be stored in one variable?
Kirby Fears
Kirby Fears le 19 Oct 2015
Modifié(e) : Kirby Fears le 19 Oct 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by