从csv文件中绘制数据在matlab中,有日期? [英] Plotting data in matlab from csv file, with dates?

查看:609
本文介绍了从csv文件中绘制数据在matlab中,有日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是两个部分:


  1. 我试图从matlab中的CSV文件中的图形数据:我到目前为止:

      filename = fopen('\fileDir.csv\','rt'); 
    FILE = textscan(filename,'%s%f%f','delimiter',',''HeaderLines',1);
    Date = FILE {1};
    Start = FILE {2};
    Stop = FILE {3};

    所以我试图 plot(Date,Start)在MATLAB。显然这不会工作,因为 Date 是一个字符串。 csv 文件中的日期格式为:<$ c $

    c> 9/1/2014 , 10/12/2014 12/5/2014 ,etc ...(因此在月和日没有前面的零)。


  2. 假设我的 csv 文件的长度为100.(日期开始 Stop length为100)。我将创建一个依赖于变量 n 的新数组。 NEW_ARRAY 长度为80,如果 n 为20,则 NEW_ARRAY length为85, n 为15.然后我想要 plot(Date,NEW_ARRAY)它们将是不同的长度,我想要 Date 从第16个( n + 1 )元素开始。

    您可以在MATLAB中绘制日期,首先使用 datenum 。然后,您可以使用 datetick 将标记标签显示为格式化日期。



    因此,我们可以用

    绘制您的数据

      plot(datenum(Da​​te),Start); 
    datetick('x',20);

    其中 20 是特定 dateformat 标识符。



    为了使用数据光标在图表上显示日期,我们需要编辑 datacursormode ,并添加我们自己的'UpdateFcn'。我们可以这样写:

      function output_txt = datacursordate(〜,event_obj)
    pos = get event_obj,'Position');
    output_txt {1} = ['X:'datestr(pos(1),20)];
    output_txt {2} = ['Y:'num2str(pos(2))];
    end

    ,然后将其应用到 datacursormode

      h = 
    plot(datenum(date),start)
    datetick('x',20)
    dcm_obj = datacursormode(h)
    set(dcm_obj,'UpdateFcn',@datacursordate)






    对于你的第二个问题,我们可以通过使用日期(n + 1:结束)索引 Date 。这将在(i + 1) th元素开始使用 Date ,并一直继续,直到 end 。

      plot(datenum(Da​​te(n + 1:end)),NEW_ARRAY); 
    datetick('x',20);


    So my question is in two parts:

    1. I am trying to graph data from CSV file in matlab: Here is what i have so far:

      filename = fopen('\fileDir.csv\', 'rt');
      FILE = textscan(filename, '%s %f %f', 'delimiter', ',' 'HeaderLines', 1);
      Date = FILE{1};
      Start = FILE{2};
      Stop = FILE{3};
      

      So I am trying to plot(Date, Start) in MATLAB. Clearly this wont work, since Date is a string. How should I approach changing the code?

      Date inside the csv file is in the format: 9/1/2014, 10/12/2014, 12/5/2014, etc...(so there are no preceding zeros in month and day).

    2. With this data I do some calculation. Lets assume my csv file has length 100. (Date, Start, Stop length is 100). I would create a new array dependent on variable n. NEW_ARRAY length would be 80, if n is 20, if NEW_ARRAY length is 85, n is 15. Then I would want to plot(Date, NEW_ARRAY), but since they would be different lengths, I want Date to start at 16th (n+1) element.

    解决方案

    You can plot dates in MATLAB by first converting them to numeric data with datenum. Then you can use datetick to display the tick labels as formatted dates.

    So we could plot your data with

    plot(datenum(Date), Start);
    datetick('x', 20);
    

    where 20 is a specific dateformat identifier.

    In order to display the dates on the graph using the Data Cursor we need to edit the datacursormode and add our own 'UpdateFcn'. We can do this by writing a function as

    function output_txt = datacursordate(~, event_obj)
        pos = get(event_obj,'Position');
        output_txt{1} = ['X: ' datestr(pos(1), 20)];
        output_txt{2} = ['Y: ' num2str(pos(2))];
    end
    

    and then applying this to the datacursormode with

    h = figure;
    plot(datenum(date), start)
    datetick('x', 20)
    dcm_obj = datacursormode(h);
    set(dcm_obj, 'UpdateFcn', @datacursordate)
    


    As for your second problem we can do this by indexing Date with Date(n + 1:end). This will start using Date at the (i + 1)th element and continue all the way until the end of Date.

    plot(datenum(Date(n + 1:end)), NEW_ARRAY);
    datetick('x', 20);
    

    这篇关于从csv文件中绘制数据在matlab中,有日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆