필터 지우기
필터 지우기

Comma Separated Imported Data

조회 수: 9 (최근 30일)
Cameron Power
Cameron Power 2018년 6월 12일
댓글: Cameron Power 2018년 6월 14일
I have a csv file that is separated by commas without spaces and with will not be separated into multiple variables. Is there something I can do in Matlab? I tried tooling with the delimiter options but to no evail, thank you (I have attached one of the files).

채택된 답변

Walter Roberson
Walter Roberson 2018년 6월 13일
S = fileread(FileName);
S(S == '"') = [];
fmt = '%f%f%f%f%f%f';
data = cell2mat( textscan(S, fmt, 'HeaderLines', 1, 'Delimiter', ',', 'CollectOutput', true) );
Now data is a numeric array with 6 columns.
Potentially you might want to use
dd = data(:,1:4);
dd(:,5:6) = 0; %add minutes and seconds column
dt = datetime(dd);
Now dt would express the dates and times as datetime objects. That can be useful in analysis, such as
Wind_Direction = data(:,5);
Windspeed = data(:,6);
T = timetable(Wind_Direction, Windspeed, 'Rowtimes', dt);
and then you can do things like
retime(T, 'daily')
which would give you the daily average wind direction and windspeed.
(However, this in itself would not know about wrapping directions at 360 so the average wind direction could be wrong, such as near 2016,01,24,00)
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 6월 13일
FileName = 'test.csv';
Cameron Power
Cameron Power 2018년 6월 14일
Brilliant, thank you all.

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

추가 답변 (1개)

Jan
Jan 2018년 6월 13일
Remove the double quotes at first:
S = fileread(FileName);
S(S == '"') = [];
fid = fopen(FileName, 'w');
fwrite(fid, S, 'char');
fclose(fid);
Then the standard tools should word: readtable, csvread, sscanf, textscan.
  댓글 수: 2
Cameron Power
Cameron Power 2018년 6월 13일
Forgive me if I am being oblivious but how do I implement this?
Walter Roberson
Walter Roberson 2018년 6월 13일
... That is the implementation, other than the fact that you would proceed from there to
T = readtable(FileName);

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by