How Does ANSYS APDL Read EXCEL Files?

In APDL, we commonly use the *Vread command to read data files. When using this command, the data reading format must immediately follow. As the data content varies, the format may also differ, which leads to poor portability of the command and many requirements for generating data files. Typically, matlab or Python is needed to clean up the format.

This article introduces a method for APDL to read EXCEL files. Since EXCEL is highly editable and this method has no requirements on data format, to a certain extent, it can free everyone from format troubles!

The main command for reading EXCEL is *TREAD. The main function of this command is to read data from an external file and convert it into a Table array. Note that it is not an ordinary Array. You can understand it as converting to a table array rather than the numeric arrays we commonly use.

Its help is as follows:

Simulation Technology | How Does APDL Read EXCEL Files?

This command only requires four parameters: the name of the table array, the name of the data to read, the file extension, and the number of lines to skip when starting to read.

Below demonstrates the basic operation method using such a file.

Given an excel file named myplot.xls, with content as shown below, totaling three columns: Time, X, Y, with approximately 300 rows. The exact number of rows is unknown. Now the data needs to be read into APDL for convenient loading.

Simulation Technology | How Does APDL Read EXCEL Files?

Step 1: First save the EXCEL file as a CSV file. You may ask why this is necessary. The main reason is that EXCEL is a binary file primarily used in Office or WPS, while CSV is a plain text file suitable for any text software.

Step 2: Define a Table array. Before defining, you first need to know the dimensions of the data. If the user does not want to manually check the data dimensions, the /INQUIRE command can be used to query the number of rows in the file data.

The code is as follows:

/INQUIRE,NumL,Lines,myplot,csv

After running the above command, check the value of variable NumL and compare it with the original data. They match!

Simulation Technology | How Does APDL Read EXCEL Files?

Define array:

*DIM,mytable,Table,NumL-1,2,1

Here let’s discuss the method for determining array dimensions. Since the first row of data is identifiers, namely Time, X, Y, we need to subtract 1 from the total number of rows. Additionally, when reading data into a table, by default the first row of data serves as the Column index value and the first column serves as the Row index value, and both must be in ascending order. We actually only need the X and Y data, so the number of columns for storing data is 2. At the same time, we need to manually add a row of index values (any value, as long as ascending order is maintained), so the CSV data needs to be modified as follows.

Simulation Technology | How Does APDL Read EXCEL Files?

Step 3: Read the CSV file content

The code is as follows:

*Tread,mytable,myplot,csv,,1

The 1 here means the first row is not read, and reading starts directly from the second row. After successful reading, open the data display of the table, as shown below:

Simulation Technology | How Does APDL Read EXCEL Files?

From this, it can be seen that the data has been completely read in.

To test data format issues, we add a new column here, expressed in scientific notation, as shown below:

Simulation Technology | How Does APDL Read EXCEL Files?

The modified command stream is as follows:

/INQUIRE,NumL,Lines,myplot,csv

*DIM,mytable,Table,NumL-1,3,1

*Tread,mytable,myplot,csv,,1

Simulation Technology | How Does APDL Read EXCEL Files?

From the figure, it can be seen that although the source data formats are different, they can still be read. Simple and fast!

This article is unauthorized for reproduction on other third-party platforms!

4. Data Reference

Although the above operations read the data in, since it is a table array, its reference is directly interpolated based on index values rather than simply referenced by row and column numbers, which is very inconvenient. Therefore, we also need to convert it to an ordinary array for convenient daily operations.

Here we need another APDL command, *VFUN. The function of this command is variable operation, among which there is a copy function that is very suitable for our operation here.

The command stream is as follows. Note that the table array cannot start from 0, otherwise the index values will be written in:

*dim,myarray,array,NUML,3

*Vfun,myarray(1,1),copy,mytable(1,1)

*Vfun,myarray(1,2),copy,mytable(1,2)

*Vfun,myarray(1,3),copy,mytable(1,3)

Through the above operations, we have converted a table array into a numeric array, which can then be specifically called by the row and column numbers of elements.

For example, perform variable assignment and compare with the original data:

aa=myarray(5,2)

*status,aa

Simulation Technology | How Does APDL Read EXCEL Files?

Through the above operations, you can perfectly achieve importing data from EXCEL to APDL, and never worry about format issues again. Solid!

The full commands are as follows:

/INQUIRE,NumL,Lines,myplot,csv
*DIM,mytable,Table,NumL-1,3,1
*Tread,mytable,myplot,csv,,1
*dim,myarray,array,NUML,3
*Vfun,myarray(1,1),copy,mytable(1,1)
*Vfun,myarray(1,2),copy,mytable(1,2)
*Vfun,myarray(1,3),copy,mytable(1,3)
aa=myarray(5,2)
*status,aa

Additionally, here is a plot using table data:

*vplot,mytable(1,1),mytable(1,2)
Simulation Technology | How Does APDL Read EXCEL Files?

If you find this useful, feel free to repost and share!

© Copyright Notice
THE END
喜欢就支持一下吧
Likes0 Share
评论 Be the First to Comment

Please log in to comment

    No comments yet