Announcement

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

  • fputmatrix(): 3250 type mismatch

    Dear Statalisters

    Could someone check the following function? Last year, I was able to run a similar function for exporting a structure to a file, but now the fputmatrix() keeps reporting the type mismatch error. It works for matrices and classes but not for structures as it did.
    I am using Stata 15.1 just updated on Windows server 2012 R2.


    Update: I just tried it using Stata 11.2 again on Windows server 2012 R2 and it works nicely. I really do not understand what is going on...

    Many many thanks.
    Best,
    Federico



    clear all

    mata

    struct _le {
    real matrix pop_le40
    }

    struct _le check()
    {

    struct _le rowvector _4le
    _4le = J(1, 2, _le())

    _4le[1].pop_le40 = J(10, 10, 1)
    _4le[2].pop_le40 = J(10, 10, 2)

    _4le[1].pop_le40
    _4le[2].pop_le40

    filename = "Z:\Microsimulation\BPCOmicrosim\analysis\bpco \che ck"

    fh = fopen(filename, "rw")
    fputmatrix(fh, _4le)
    fclose(fh)

    return(_4le)
    }

    end

    m struct_res = check()

    Last edited by Federico Belotti; 10 Sep 2018, 03:46.
    Federico

  • #2
    What you discovered is a bug which was introduced in Stata 14 when we added ability to save Mata class matrix with fputmatrix() and tried to make saving Mata struct more robust. We will fix the bug in a future Stata executable update. Meanwhile, you may temporarily work around the issue by changing struct to class.

    Example:

    Code:
    cscript
    
    mata:
    
    class _le {
        real matrix pop_le40
    }
    
    class _le check()
    {
    
    class _le rowvector _4le
    _4le = J(1, 2, _le())
    
    _4le[1].pop_le40 = J(10, 10, 1)
    _4le[2].pop_le40 = J(10, 10, 2)
    
    
    filename = "test.mat"
    
    fh = fopen(filename, "rw")
    fputmatrix(fh, _4le)
    fclose(fh)
    
    return(_4le)
    }
    
    end
    
    mata: 
        cls_res = check()
        filename = "test.mat"
        fh = fopen(filename, "rw")
        res = fgetmatrix(fh)
        fclose(fh)
    
        assert(rows(res)==1)    
        assert(cols(res)==2)
        assert(res[1].pop_le40==J(10, 10, 1))
        assert(res[2].pop_le40==J(10, 10, 2))    
    end

    Comment


    • #3
      Ok, this explains why the function works in Stata < 14. Many thanks for the workaround.

      Best
      Federico
      Federico

      Comment

      Working...
      X