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

4 views (last 30 days)
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 Comments
Guillaume
Guillaume on 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.

Sign in to comment.

Accepted Answer

Kirby Fears
Kirby Fears on 16 Oct 2015
Edited: Kirby Fears on 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 Comments
Kirby Fears
Kirby Fears on 19 Oct 2015
Edited: Kirby Fears on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data 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!