Ansys Assistant will be unavailable on the Learning Forum starting January 30. An upgraded version is coming soon. We apologize for any inconvenience and appreciate your patience. Stay tuned for updates.
General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

Import .mat file

    • MollyC
      Subscriber

      Good morning,


      I have a matrix as a ".mat" file that I want to import to Ansys APDL or import just the individual values from the matrix at a time.


       


      Thank you for your help!

    • jpasquerell
      Ansys Employee

      The *DMAT and *SMAT commands have an option to import a .MAT file.  They are designed to work with a .MAT file created by the ANSYS *EXPORT command.  If the .MAT file is an ASCII text file you could use *VREAD or *TREAD depending on the format used to create it.  If tab or comma delimited then *TREAD is likely needed. Otherwise you need to give info on what program wrote the file and whether it is binary or text. If binary then use of user programmable features may be required.

    • peteroznewman
      Subscriber

      Molly, I am guessing that matlab wrote the .mat file, right?


      In that case, open matlab and use a short script to write the matrix to a tab delimited text file.


      You will have a much easier time importing a text file to ANSYS than the matlab binary .mat file format.

    • MollyC
      Subscriber

      Hi Peter, 


      yes matlab wrote the .mat file. When I use a text file for the matrix can I tell ANSYS that for example I just want the value from the 6 row and the 4 column?


      So is there a command for a specific value (or on a specific spot) in a text file?


       


      Thank you for the Answer

    • MollyC
      Subscriber

      Hi jpasquerell,


      how can I use the *EXPORT command when I didn't import the matrix before? It's a matrix with Double precision real values.


       


      Thank you for the Answer

    • peteroznewman
      Subscriber

      Hi Molly,


      When I need one number from matlab, I use the Windows clipboard to copy from matlab and paste into ANSYS. If you double click on a variable name in matlab, it opens the variable in the Variables window and you can click on the one cell and copy that.  Use the matlab View tab to set the Number Display Format to Long Fixed Decimal or Long Scientific Notation to get all the double precision digits.


      If you must write the number to a file in a matlab script, then just modify the previous script and write out A(3,4) for example.


      Jpasquerell means that ANSYS has a lot of matrices it uses, and you can export them out to an ANSYS .mat file, which is different to a matlab .mat file, but your original post did not mention matlab so he assumed you were referring to ANSYS .mat files. I guessed you were talking about matlab.

    • MollyC
      Subscriber

      Hi Peter,


      ok that sound simple but I have a matrix that is pretty big (1000 x 1000) and I wanted to write a loop. And in this loop I wanted to save the value from the x row and the y column as a specific thickness. 


      Thank you for the Answer


       

    • peteroznewman
      Subscriber

      Hi Molly,


      If x = 357 and y = 468 and the matrix is variable M you can get out the value like this:  M(357,468)  and you can assign that to a variable t if you like.


      t = M(357,468);


      or if x and y were variables,


      t = M(x,y);

    • MollyC
      Subscriber

      Hi Peter,


       


      Ok but don't I need to import the matrix first to assign t as M(x,y)? And I still can't work out how to import the matrix as .mat file from MatLab.


       


      Thank you for your answer! 

    • peteroznewman
      Subscriber

      Hi Molly,


      The code above is executed in matlab where the matrix M exists and all you want is a single value at a known row and column. Once you have that value, you can write it to a file or copy it to the clipboard to get it into ansys.  You don't need the whole matrix in ansys, unless you need to extract different values in some program loop in ansys. Is that what you have in mind?

    • sk_cheah
      Subscriber

      Hi Molly,


      What jpasquerell recommended is described well by SimuTech using *TREAD and *VREAD. An alternative strategy is to write the file to Matrix Market Format. First download mmwrite.m script from NIST.gov website. You could then export the matrix from Matlab using these Matlab commands : 


      m = transpose(1:10) * (1:10);  % example matrix
      mmwrite('toansys.mtx', m, 'example matrix'); % writes out matrix to file

      In Ansys, the APDL script to import the file : 


      *dmat, importmatrix, d, import, mmf, toansys.mtx ! imports double precision MTX file
      *dim, fromMatlab, array, 10, 10 ! pre-define matrix size
      *export, importmatrix, apdl, fromMatlab, 1, 10 ! writes it out to apdl array named fromMatlab, 1st column to 10th column

      Now you may access the values from the imported matrix in Ansys. An example: 


      q = fromMatlab(3, 4) 
      *stat, q


      Kind regards,
      Jason

    • MollyC
      Subscriber

      Hi Peter,


      yes I want to write a loop so I need the hole matrix in ANSYS. 


       


      and


       


      Hi Jason,


      Ok I'll try that out, but isn't there a simpler way to import that matrix?


       


      Thank you for your answer!

Viewing 11 reply threads
  • The topic ‘Import .mat file’ is closed to new replies.
[bingo_chatbox]