Announcement

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

  • Word function

    Hello. I am trying to format the cell borders of a 7x18 table using the function _docx_cell_set_border() as described in this page. However, i get an error. Is this function not available in Stata 13?

  • #2
    You are right, the function _docx_cell_set_border() is added in Stata 14.

    Comment


    • #3
      Is there a way to get this function in Stata 13? The idea is to create a formatted table directly and not being able to control the borders defeats the purpose. Alternatively, is there any workaround that you could suggest?

      Also, when I load a table from a (Stata or Mata) matrix using the respective function, a line is added in each cell. Is this a bug and is there a way to prevent this from happening?

      For example (space denoted by <space>):
      <space>
      50
      <space>
      60
      <space>
      70
      instead of:
      50 60 70
      Last edited by Belinda Foster; 20 Feb 2017, 11:52.

      Comment


      • #4
        I am now trying to limit the use of the above function to Stata 14 using the following code:

        Code:
        if (stataversion() >= 1400) {
                  m = _docx_cell_set_border(dh, tid, 5, 8, "top", "solid", "black")
                  }
        However, i get the following error when I run it in Stata 13: 3499 _docx_cell_set_border() not found.

        Why is this happening? Shouldn't Mata just ignore this portion of code? Is there a way to deal with this?
        Last edited by Belinda Foster; 26 Mar 2017, 18:28.

        Comment


        • #5
          Mata is compiled, not interpreted, and so it will try to compile that section of code where it encounters a function that cannot be found prior to Release 14. I don't recall any compiler directives that would allow conditional compilation of code in Mata.

          Comment


          • #6
            Hi
            The problem is that the mata code only can be run if the version of Stata is proper. I've had the same problem with xl()
            Below if the Stata version is above 13 the code look for my mata file with xl() commands and run it.
            Then and only then can I refer to the code using xl():
            Code:
                    if `c(stata_version)' > 13 {
                        mata: st_local("toxl_exists", findexternal("__basetable_to_xl()") ? "1" : "0")
                        capture quietly findfile "basetable2xl.mata", path(`".;`c(adopath)'"')
                        if !_rc & !`toxl_exists' run `r(fn)'
                        mata: st_local("toxl_exists", findexternal("__basetable_to_xl()") ? "1" : "0")
                        if `toxl_exists' {
                            mata: __basetable_to_xl(tbl, "`toxl'")
                            display "Table send to Excel succesfully..." _n
                        }
                        else {
                            display "{error:Table NOT send to Excel!!! Code for toxl missing.}" _n
                        }
                    }
            It is not pretty, but it works
            And it can be formulated strictly in Mata, if that is required
            Kind regards

            nhb

            Comment


            • #7
              Niels Henrik Bruun thanks but what I want is to only execute a few lines of code when Stata is 14 or later. Not to check the Stata version first and then decide whether to execute or not the entire Mata function. Is this possible? I cannot tell form the code your attached in your post.

              And it can be formulated strictly in Mata, if that is required
              Also it is not clear what you are trying to suggest above. Do you mean I could include the relevant lines of mata code in a separate second function and call this from within the first function?

              Comment


              • #8
                I'm no expert, but so far as I know you can not call any Stata 14 Mata code in Stata 13.
                As soon as you do you get an error.
                So what I've done is that if I'm using Stata 14 then I parse the Stata 14 Mata code and I can call it.
                If I'm in Stata 13 the code is never parsed and hence never causing any trouble.

                What I'm suggesting is that it should be quite easy to do what I've done strictly in Mata. Most of my code is already Mata.
                And in your code example contain the version control.

                I have the same problem with xl(). I eg can better control the appearance of Excel spreadsheets in Stata 14.
                I think it is good that it is only a few lines. Then it is easier to maintain.

                So my advice is to keep as much as possible of your Mata as Stata 13 (Or as early as you want to go back)
                Then do a mlib of that code in Stata 13.
                Isolate the little Stata 14 Mata code that is left in a separate Mata file and do a late parsing on that part.

                Coming to think of it you might be able to use capture in Stata to work around your problem.

                Look at this code which running in Stata 13
                Code:
                . capture rm Test.xls
                
                . mata:
                ------------------------------------------------- mata (type end to exit) --------------------------
                :         mata clear
                
                : 
                :         real scalar try_mata_code(string scalar matacode) 
                >         {
                >                 return(_stata(sprintf("mata %s", matacode)))
                >         }
                
                :         xl = xl()
                
                :         xl.create_book("Test", "test")
                
                :         rc = try_mata_code(`"xl.set_column_width(1, 1, 15)"')
                function set_column_width() not declared in class xl
                
                :         
                :         rc
                  3000
                
                :         printf("It works")
                It works
                : end
                The function try_mata_code() makes Stata 13 handle Stata 14 code in Stata 13 without stopping the code running.
                This is simpler!
                Kind regards

                nhb

                Comment


                • #9
                  @Stata: A try-except functionality in Mata would be even better!
                  Kind regards

                  nhb

                  Comment


                  • #10
                    Niels Henrik Bruun have you come across a bug with the _docx_cell_set_border function? It appears that in Stata 13 artifacts are introduced when any of the options thick, double, thickThinSmallGap, thinThickLargeGap etc is selected for the top border.

                    Can someone from StataCorp confirm that this is a bug or is it perhaps something that I am doing wrong? It seems to be working when the bottom border is defined instead.
                    Last edited by Belinda Foster; 03 May 2017, 07:05.

                    Comment


                    • #11
                      Belinda Foster : I do not use _docx yet
                      Kind regards

                      nhb

                      Comment


                      • #12
                        These functions appear to have quite a few bugs. For example, the auto option for _docx_table_set_width does not work either.

                        Comment

                        Working...
                        X