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

7 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2015-10-16
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.

请先登录,再进行评论。

采纳的回答

Kirby Fears
Kirby Fears 2015-10-16
编辑:Kirby Fears 2015-10-16
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 个评论
Kirby Fears
Kirby Fears 2015-10-19
编辑:Kirby Fears 2015-10-19
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by