Read all the columns in a .csv file

조회 수: 11 (최근 30일)
Damith
Damith 2014년 10월 29일
댓글: Damith 2014년 11월 19일
Hi,
I have a .csv file with the following columns, I need to read all the columns. What function I need to use. I tried csvread but it did not work.
KH 110427 PH M 1951-01-01T07:00:00+07:00 0 mm O
KH 110427 PH M 1951-01-02T07:00:00+07:00 0 mm O
KH 110427 PH M 1951-01-03T07:00:00+07:00 0 mm O
.
.
.
Any ideas?
Thanks.

채택된 답변

Star Strider
Star Strider 2014년 10월 29일
I would experiment with textscan or perhaps fscanf. Not having the file I can’t be more specific.
  댓글 수: 18
Star Strider
Star Strider 2014년 11월 4일
You have to do the loop and integrate my code into it. I have no idea what you want to do or in what order you want to do it.
Damith
Damith 2014년 11월 19일
I wanted to modify the code below to show the mm/dd/yyyy in first column if col 6 of each cell has complete data set (i.e. if number of days per year = 365 or 366) but not incomplete years and col 6 values of each cell in second column in a diferent cell array "newyr".
Do you have any idea?
Thanks in advance again.
tr = readtable('test.csv','ReadVariableNames',0); % Load Data
% td = tr(1:5,:) % Diagnostic Write
isnneg = @(x) x>=0; % Function
tc = table2cell(tr);
valrow = cellfun(isnneg,tc(:,6)); % Col #6 >= 0
tcval = tc(valrow,:); % Logical Vector
% tcvq = tcval(1:5,:) % Diagnostic Write
tcdn = datenum(tcval(:,5), 'yyyy-mm-ddTHH:MM:SS'); % Create Date Numbers
tcdv = datevec(tcdn); % Create Date Vectors
% tcdq = tcdv(1:5,:); % Diagnostic Write
[uy,days,~] = unique(tcdv(:,1)); % Years In File
dend = diff([days; length(tcdn)]); % Lengths Of Years In File
yrbgn = tcdv(days,:); % First Days Of Years
yrend = tcdv([days(2:end)-1; length(tcdn)],:); % Last Days Of Years
yrvld1 = find((yrbgn(:,2) == 1) & (yrbgn(:,3) == 1)); % Valid Year Starts
yrvld2 = find((yrend(:,2) == 12) & (yrend(:,3) == 31)); % Valid Year Ends
yrvldix = yrvld1(ismember(yrvld1, yrvld2)); % Valid Years
yrvldds = days(yrvldix); % #Days In Valid Years
for k1 = 1:length(yrvldix) % Create Output Year Data
yrout{k1} = tcval(days(yrvldix(k1)):days(yrvldix(k1))+dend(yrvldix(k1))-1, :);
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 29일
If you make the first row a line of tab separated names for the columns....
col1 col2 col3 col4 col5 col6 col7 col8
KH 110427 PH M 1951-01-01T07:00:00+07:00 0 mm O
KH 110427 PH M 1951-01-02T07:00:00+07:00 0 mm O
KH 110427 PH M 1951-01-03T07:00:00+07:00 0 mm O
Then you could use readtable() which gives a table rather than a cell array. I find tables are easier to use than cell arrays where you always have to figure out whether you want to use braces or parentheses.
t=readtable('test2.csv', 'delimiter', '\t')
When I ran it.....
>> test2
t =
col1 col2 col3 col4 col5 col6 col7 col8
____ __________ ____ ____ ___________________________ ____ ____ ____
'KH' 1.1043e+05 'PH' 'M' '1951-01-01T07:00:00+07:00' 0 'mm' 'O'
'KH' 1.1043e+05 'PH' 'M' '1951-01-02T07:00:00+07:00' 0 'mm' 'O'
'KH' 1.1043e+05 'PH' 'M' '1951-01-03T07:00:00+07:00' 0 'mm' 'O'
  댓글 수: 8
Damith
Damith 2014년 10월 30일
Thanks again Image Analyst. All csv files I received from an agency and they have created the csv files. All I need to read the files, which I have a code to read multiple csv files but it does not read as in the structure shown in test.csv. But the code you provided earler works fine the way I want but your test2.csv file is different from my .csv files. I dont know how you converted from test.csv to test2.csv.
Image Analyst
Image Analyst 2014년 10월 30일
I just did it manually in the text editor. If you wanted to ad a preprocessing step where you open all the files and add that headerline, you could easily do that with the code in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by