如何在 matlab 中导入带有日期的 csv 文件并绘制它?

How to import a csv file with date in matlab and plot it?

我有一个包含两列的 csv 文件,日期和浮点数,日期格式很奇怪(2016 年 1 月 1 日 9:55:00 下午),如何将其导入 matlab 并将其绘制为绘图?这是我试过的:

fid = fopen('all.csv');
if fid>0    
     data = textscan(fid,'%s %d','Delimiter',',');
     % close the file
     fclose(fid);         
end
x = data(:,1);
y = data(:,2);

plot(x,y);

但是我得到一个参数不足的错误

all.csv 样本:

Jan 1 2016 9:55:00 PM, 12493829,
Jan 2 2016 7:55:00 AM, 83747283,
Jan 3 2016 2:55:00 PM, 89572948,
Jan 4 2016 8:55:00 AM, 95862744,

错误:

Error using plot
Not enough input arguments.

Error in test (line 10)
plot(x,y);

出现此错误是因为您在 plot 函数中输入元胞数组。

x = data{:,1};   %You need { } here, not ()
y = data{:,2};

plot(1:numel(x), y, 'o-');
%Ensuring the xticks are as required and changing the xticklabels to datetime values and 
%rotating the labels for better visualisation ('xticklabelrotation' requires >= R2014b)
set(gca,'xtick',1:numel(x),'xticklabel',x,'xticklabelrotation',45);  

输出:

fid = fopen('all.csv');
if fid>0    
     data = textscan(fid,'%s %d','Delimiter',',');
     % close the file
     fclose(fid);         
end
x = datenum( data(:,1),'mmm dd yyyy HH:MM:SS PM');Convert it to a compatoble format before
y = data(:,2);
plot(x,y);
datetick('x','dd.mm.yyyy','keepticks') %Makes the x axis numbers to dates