Read all the columns in a .csv file

7 ビュー (過去 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 Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by