Borrar filtros
Borrar filtros

import a part data from a csv file

8 visualizaciones (últimos 30 días)
Ken
Ken el 20 de Jul. de 2012
I worked on importing csv files for days, but still quite new to Matlab. Sorry for bringing this kind of question again, I did not find an exact answer to my work. I listed my file my code, and hope can get help :). Thanks.
1. csv dataset looks like;
2. my code to do the import;
3. Data I want to have;
4. questions;
------------------------------------------------------
------------------------------------------------------
1. Dataset looks like:
This csv file is a double-quoted format file. I want to import data from lines below the line with "DataType" and "NET". Until it reads a "blank line". Data should be read into 'NET', 1st column is character, other columns are numeric. Tried to read NET into matrix but fail, but I can read it into cell array type. Please also see Matlab codes in 2.
------------------------------------------------------
"date:", "XX XX"
"Operator:", "XXXX"
" "
"XXXXXXXX", "XXXX", "YYYY", "NNNN"
"DataType", "NET"
"a", "b", "c", "d", .. "x"
"v1", "2", "2", "3", .. "1"
"v2", "2", "2", "3", .. "1"
"v3", "3", "2", "3", .. "1"
"v4", "2", "6", "3", .. "0"
"v5", "2", "2", "3", .. "1"
...
"v115", "2", "2", "3", .. "1"
<---------------- (blank line here)
"other", "other"
...
"other", "2", "2", "3", .. "1"
"other", "2", "2", "3", .. "1"
----------------------------------------------------
2. My code to do the import:
fid_t = fopen('C\:data.csv', 'r');
while ~feof(fid_t)
temp1 = fgetl(fid_t);
temp2 = regexprep(temp1, '"', '');
line = csv2cell(temp2);
if strcmp(line{1}, 'DataType:')
if strcmp(line{2}, 'NET')
O2=line{2};
fgetl(fid_t);
NET={};
while ~feof(fid_t)
if isempty(line)
break;
end
tp1 = fgetl(fid_t);
tp2 = regexprep(tp1, '"', '');
temp_1 = textscan(tp2, '%s%s%s%s%s%s%s%s%s',...
'Delimiter', ',');
temp_2=[temp_1{:}];
NET{end+1}=temp_2;
end
end
end
end;
fclose(fid_t);
3. Data I want to have
------------------------------------------------
NET =
v1 2 2 3 .. 1
v2 2 2 3 .. 1
v3 3 2 3 .. 1
v4 2 6 3 .. 0
v5 2 2 3 .. 1
...
v115 2 2 3 .. 1
-------------------------------------------------
4. Questions:
1)My code in (2.) worked, and could return a NET cell array for me. But I got error:
??? Error using ==> textscan First input can not be empty.
NET:
NET= {1x9 cell} {1x9 cell} {1x9 cell}....{1x9 cell} ....
NET <1x96 cell>
But how to convert NET to a matrix looks like in 3?
2)Is there any effective way to do this? Columns in NET are not fixed. I have a batch of such kind of files, some of them have only 4~5 columns and some of them have 50~60 columns. I do want to read all these columns into matrix and make it looks like in 3.
Thank you so much.

Respuestas (0)

Categorías

Más información sobre Numeric Types 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