필터 지우기
필터 지우기

Split into different columns a .csv file with characters ';'

조회 수: 18 (최근 30일)
Ancalagon8
Ancalagon8 2017년 4월 30일
댓글: Ancalagon8 2017년 5월 3일
I have a large .csv file with the below format ('Date;Time;Value').
'16/4/2017;05:12:02;1.21'
and need to import it into 3 collumns.
'16/4/2017' '05:12:02' '1.21'
My code is:
fid = fopen('KD.csv','r');
C = textscan(fid, '%s %s %f %f %s', 'HeaderLines',1, 'CollectOutput',true);
fclose(fid);
[dt,val,exch] = deal(C{:});
w=regexp(dt,'\s+','split')
out=reshape([w{:}],1,[])'
time = [dt regexp(dt, '\d\d:\d\d:\d\d', 'match', 'once')]
Any help?

채택된 답변

Guillaume
Guillaume 2017년 5월 1일
Even better than xlsread, readtable would be a much better and modern option. If the header line that is skipped actually contains columns names readtable could even parse these. Skipping it, as in the original code:
kd = readtable('KD.csv', 'delimiter', ';', 'ReadVariableNames', false, 'HeaderLines', 1);
kd.Properties.VariableNames = {'dt', 'val', 'exch'}; %name the columns of the table
Note that readtable is usually clever enough to detect the delimiter, number of lines to skip and whether or not the variable names are included, so:
kd = readtable('KD.csv');
may be enough.
  댓글 수: 3
Guillaume
Guillaume 2017년 5월 2일
"thanks a lot! "readtable" did exactly what i needed!"
So why did you unaccept the answer? Because it didn't answer your completely different other question which wasn't asked in the first place?
"i should convert time from non-numeric form to what?"
If readtable didn't already convert your time to datetime. You can do it manually,
kd.val = datetime(kd.val, 'InputFormat', 'HH:mm:ss');
Modern versions of matlab know how to plot against datetime.
Ancalagon8
Ancalagon8 2017년 5월 3일
@Guillaume Yes. I did manually. Thanks again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Pulse and Transition Metrics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by