Reading text data from a CSV file in MATLAB

前端 未结 5 1305
-上瘾入骨i
-上瘾入骨i 2020-12-21 14:37

my data is in following form:

days of week      date        time(hrs)        visitors
mon            jan 2 2010     900               501 
mon            jan         


        
5条回答
  •  再見小時候
    2020-12-21 14:54

    Here is how I would read the tab-separated values, and parse the dates:

    %# read and parse file
    fid = fopen('data.csv','rt');
    C = textscan(fid, '%s %s %s %d', 'Delimiter','\t', 'HeaderLines',1, ...
        'MultipleDelimsAsOne',true, 'CollectOutput',false);
    fclose(fid);
    
    %# get date and number of visitors
    dt = datenum(strcat(C{2}, {' '}, C{3}), 'mmm dd yyyy HHMM');
    visitors = C{4};
    
    %# plot
    plot(dt,visitors)
    datetick('x')
    xlabel('time of day'), ylabel('visitors')
    

    enter image description here

    As for the day-of-week column, you can get it as:

    >> C{1}                        %# first column from file
    ans = 
        'mon'
        'mon'
        'mon'
    
    >> cellstr(datestr(dt,'ffffd'))  %# actual day of week from parsed dates
    ans = 
        'Sat'
        'Sat'
        'Sat'
    

    this produces different days (either your data posted was simply made-up, or you have a bug in the part that generated those dates!)

提交回复
热议问题