Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • convert a matrix to a column vector, and then output it to Excel file

    Hello, I need your help on matrix and Excel in Stata.
    I have a matrix m and would like to stack its columns, making it a column vector and then output it to excel. How may I do it? I got an error message but do not know how to proceed. Your help is highly appreciated.

    mata

    m1 = colshape(st_matrix("m"), 1)

    end

    getmata m1
    export excel using test.xlsx, replace

    putexcel set results211117.xlsx, sheet(a, replace) modify



    This is my matrix m (The column names are 0, 1, 2,......8)

    . matrix list m

    m[10,9]
    0 1 2 3 4 5
    bxxxxxxx .52266514 .58911588 .83552992 .56056369 .8199342 .7093596
    pxxx 3.732e-17 4.505e-07 1.945e-11 8.223e-08 .00003257 2.086e-37
    pyyy 7.988e-15 .00036929 .16934102 .00002184 .35435738 3.141e-09
    impoxxxxxxxx .69417235 .97630077 1.0495413 .75846318 1.0857929 1.010838
    pxxx .00005891 3.303e-10 1.661e-06 .00010401 5.796e-06 1.058e-06
    pyyy .07332287 .87432756 .81737478 .21084674 .71513743 .95735001
    impoaaaaaaaa .35782885 .18078305 .57813267 .31135534 .46354425 .39562263
    pxxx .11893438 .47588783 .00996288 .25309767 .14475445 .06826691
    pyyy .00535643 .00136421 .05931852 .01186249 .09168185 .00553321
    pxxxxxxxxxx .38558787 .02224672 .18134953 .28309624 .12058027 .13453773


    .........




  • #2
    Code:
    . matrix define m = (11,12,13 \ 21,22,23 \ 31,32,33)
    
    . matrix list m
    
    m[3,3]
        c1  c2  c3
    r1  11  12  13
    r2  21  22  23
    r3  31  32  33
    
    . mata: m1 = colshape(st_matrix("m"), 1)
    
    . getmata m1
    
    . list, clean
    
           m1  
      1.   11  
      2.   12  
      3.   13  
      4.   21  
      5.   22  
      6.   23  
      7.   31  
      8.   32  
      9.   33  
    
    . export excel using test.xlsx, replace
    file test.xlsx saved
    
    .
    and this successfully creates a workbook called test.xlsx containing the worksheet sheet1 in which the range a1:a9 contains the expected 9 digits.

    The point is that your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. See how I presented my code and results, copied from Stata's Results window and pasted using code delimiters [CODE] and [/CODE] for clarity? Perhaps you could prepare an example of what doesn't work and show us the example and the error message. It's one thing to ask Statalist to explain an error message, quite another to ask Statalist to guess what the error message is before explaining it.

    Comment


    • #3
      Thank you very much for your answer and comments. Yes, I will be more careful and present everything more clearly.

      Comment

      Working...
      X