如何读入输入文本文件的第二列作为矩阵的值?

时间:2014-09-13 09:24:14

标签: matlab octave

我有很多包含两列的文本文件,例如:

    1998-08  56
    1998-09  78
    1998-10  12
    1998-11  76

我有一个MATLAB代码,我想用八度音程运行它。此代码生成随机矩阵(c),然后计算泊松逆,现在代替我想告诉它的随机矩阵得到文本文件的第二列作为c的值。 然后,一旦我计算出来,我想将结果绘制在图表中,其中第一列上的日期为x轴的值。 我的问题是天真的,但我非常非常新的八度音,我花了最后一天搜索文档,但我没有发现任何有用的东西。

     clc
     clear all
     #here I need to read in input the second columns of each file 
     C =     [8,9,3,6,4,1,5,8,3,5,8,10,5,3,1,5,11,3,6,5,8,5,1,3,5,3,3,7,0,2,6,7,1,8,3,5,2,6,3,5,3,8,2,5,2,7,4,4,5,50,7,5,3,3,8,2,7,7,3,6,4,4,2,4,5,1,5,14,2,6,1,4,7,3,4,6,1,3,4,4,5,1,5,4,4,6,6,2,0,4,8,9,4,5,10,3,4,2,3,6,6,8,5,5,8,3,8,4,5,4,1,4,6,5,2,5,5,7,5,1,5,4,5,3,5,11,8,6,6,0,6,4,6,5,6,4,6,4,11,4,6,5,4,4,4,2,4,7,4,40,7,7,3,1,6,7,7,4,10,5,5,4,8,5,4,5,2,7,8,6,7,6,4,3,3,3,4,6,1,7,2,6,5,7,3,6,3,6,2,3,4,7,2,4,3,6,4,7,9,5,6,2,4,4,7,4,6,3,5,3,3,5,4,8,5,3,6,4,8,6,3,8,2,7,5,3,3,8,8,4,4,8,5,7,4,4,3,6,8,4,3,4,3,9,4,3,10,4,4,5];
    C = C';

    N = length(C);
    H = 6;
    w = [1:H H+1 H:-1:1]';

    Lambda_Tilde = NaN * zeros(N, 1);
    L_Tilde = NaN * zeros(N, 1);
    U_Tilde = NaN * zeros(N, 1);
    for t = (H+1):(N-H-1)
       Lambda_Tilde(t) = sum(w .* C(t-H:t+H)) / sum(w);
       L_Tilde(t) = poissinv(0.005, Lambda_Tilde(t));
       U_Tilde(t) = poissinv(0.995, Lambda_Tilde(t));
    end

    clf
    plot(1:N, C, 'bo-');
    hold on
    plot(1:N, L_Tilde, 'r-.');
    plot(1:N, U_Tilde, 'r-.');

1 个答案:

答案 0 :(得分:1)

导入和导出数据真的是well documented,所以很难相信“发现没有什么有用”。特别是importdata对你来说很有意思。

data = importdata('Data.txt');
C = data.data.';

返回:

C =

    56    78    12    76

题外话:

NaN * zeros(N, 1)NaN(N, 1)

相同