Matlab/Octave - 解析和绘制日期字符串与整数 [英] Matlab/Octave - parsing and plotting date string vs. integer
本文介绍了Matlab/Octave - 解析和绘制日期字符串与整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据文件,第一列是日期字符串,第二列是整数:
I have a data file with the first column as date strings and the second column an integer:
"2020/02/29" 1
"2020/03/03" 2
"2020/03/04" 6
我想解析这个文件并在 x 轴上绘制日期,在 y 轴上绘制整数.我最近一次失败的尝试是:
I want to parse this file and plot the date on the x-axis and the integer on the y-axis. My most recent failing attempt is:
file_name = "data.dat";
fid = fopen(file_name, 'rt');
raw_data = textscan(fid, "%s %d");
fclose(fid);
graphics_toolkit('gnuplot');
plot(raw_data(:, 1), raw_data(:, 2));
图表为空,没有可显示的数据.如何正确绘制此类数据?似乎 textscan
的输出对于绘图来说有点笨拙.
The graph is empty with no data to show. How can I properly plot such data? It seems the output of textscan
is a little unwieldy for plotting.
推荐答案
不能绘制字符串,必须先将日期字符串转换为日期数字.使用 cellfun
来高效地做到这一点
you can't plot a string, you must convert the date string to a date number first. use cellfun
to do this efficiently
x=cellfun(@(d) datenum(regexprep(d,'"',''),'yyyy/mm/dd'), raw_data{1});
y=raw_data{2};
plot(x,y)
这篇关于Matlab/Octave - 解析和绘制日期字符串与整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文