Suppressing New-Line Characters in APDL, and a Better WRTTBL.mac

OK, it is Friday afternoon and if I do not write something soon the week will be missed. We did not do a seminar this week so I can not just post the notes and some comments from the webinar, bummer.  All of the real tech support people at PADT have been busy with training, mentoring and doing tech support, so they did not kick anything out. So that leaves me to come up with something. So, as is usually with me, I looked for something I felt guilty or ashamed of. Because that is the way my brain works.

And I remembered that two posting ago I put out a piece of junk macro that printed out tables, as part of the second article on tables in APDL.  Although it worked it was brute force and it used a bunch of *if statements to determine how many columns to write.  Ugly.

While I was extruding that particular piece of bodily waste something in the back of my mind said that APDL had an undocumented command that would suppress a line-feed on a *VWRITE. This is what one does with ‘C’ and other languages invented after the 1970’s.  If you suppress the line-feed, you can just loop over the number of columns. 

Next step, go to the help and see if it is there is some clue as to if that tickle in my brain was valid.  I found a posting on XANSYS from 2004… by some guy name Eric Miller…  Go figure. 

There are two descriptors that are not documented in the help: ‘/’ and ‘$’. 

‘/’ adds a newline, and ‘$’ suppresses it.  So if I want to write out the values in a 1D array all on one line but I don’t know how long the array is I can do:

*dim,myar,,10
myar(1) = 1,2,3,4,5,6,7,8,9,10
*get,nrw,parm,myar,dim,X

*cfopen,foo1.txt

*do,i,1,nrw
*vlen,1
*vwrite,myar(i)
(g16.9,$)
*enddo
*vwrite
(x)
*cfclose
 
This ends up creating foo1.txt:

image

So, extrapolating this, we can rewrite the nothing-to-be-proud about old wrttbl.mac with

 

ttbl = arg1      ! Get the name of the table you want to write

fname = arg2     ! get the name of the file to write to

 

*get,nrw,parm,%ttbl%,dim,X         ! Get the size of the table

*get,ncl,parm,%ttbl%,dim,Y

*get,xax,parm,%ttbl%,var,1         ! Get the names of the columns

*get,yax,parm,%ttbl%,var,2

 

*cfopen,%fname%    !Open the file

 

*vwrite,ttbl,xax,yax    ! Write a header (note / to add a line)

('Table: ',A,' ',A,' vs ',A,/) 

 

*vwrite         ! write 10 spaces, then don't write a new line by using $

('          |',$)

 

*do,jj,1,ncl ! Loop on each column, writing out the column value ($ again)

    *vlen,1

    *vwrite,%ttbl%(0,jj)

    (g10.4,$)

*enddo

*vwrite    !You need a line feed now that you are done, just write a space

(' ')

*vwrite    !Write a line of  dashes to seperate the header, with a pipe

(10x,'|',$)   ! to seperate the row values

*do,jj,1,ncl

    *vwrite

    (10('-'),$)

*enddo

*vwrite

(' ')

 

*do,ii,1,nrw    !Now write the values, looping on each row, then each column

    *vlen,1

    *vwrite,%ttbl%(ii,0)

    (g10.4,'|',$) 

    *do,jj,1,ncl

        *vlen,1

        *vwrite,%ttbl%(ii,jj)

        (g10.4,$) 

    *enddo

    *vwrite

    (' ')

*enddo

 

*cfclose

Have a great weekend!

One Reply to “Suppressing New-Line Characters in APDL, and a Better WRTTBL.mac”

  1. That was of great help! Just one thing: I’m trying to export data to a csv file. With this method, the data just goes as one whole text into the first cell of the row. Is there a way to export data to csv, and have it go into each cell of the row? Thanks!