asdocx: Export from Stata to Word, Excel, LaTeX & HTML › Forums › asdocx Forum › Creating Multiple Custom Tables at Once Using Flexmat, Then Output To One Doc?
Kevin BlaineParticipantJanuary 6, 2023 at 6:35 pmPost count: 45
Hi Dr. Shah — Is it possible to create multiple flexmat tables in a single .do file, then merge them in consecutive locations in a new .docx?
I’ve been trying a number of different approaches to get the result I’m looking for, and nothing seems to work. Here is my current iteration:
cd "/Users/Default/Downloads" asdocx setfile, save(First_Test) flexmat addrow, data(Year, Month, Day) r(1) flexmat addrow, data(2001, March, 21) r(2) asdocx setfile, save(Second_Test) flexmat addrow, data(Make, Model, mpg) r(1) flexmat addrow, data(Ford, Explorer, 15) r(2) asdocx, save(REPORT) text(MY REPORT) title replace flexmat appendmat, matname(First Test) flexmat appendmat, matname(Second_Test) asdocx export
But then I receive this error, with context from SET TRACE ON:
= mata : append_matrices("/Users/Default/Downloads/_asdoc/REPORT.flexmat", "First_File", 17) file First_File not found fopen(): 601 file not found append_matrices(): - function returned error <istmt>: - function returned error
Troubleshooting, I replaced the “First File” with its full path in the matname() option, but then receive this error:
append_matrices(): 3498 Columns are not equal in the two matrices <istmt>: - function returned error
At the end of the day, I want to be able to iteratively add tables/elements to a single report, which will include some stock asdocx tables (i.e. asdocx sum) and some custom flexmat tables. What’s the best way to do this? Create individual docx files for each table, then combine them with putdocx? Is there anyway to build the tables, save them to individual flexmat files, and then combine them in order in a new flexmat file?
The other approach I’ve tried is to take the simplest approach:
asdocx, save(REPORT.docx) text(MY REPORT) title replace flexmat addrow, data(Year, Month, Day) r(1) flexmat addrow, data(2001, March, 21) r(2) ..etc
However, since flexmat assumes a default location of 1, the report is always overwritten with the final flexmat codeblock. I know that I can tell flexmat where to put the table (e.g. loc(#), but it seems like I have to do that for every single flexmat command, and that will be difficult to do when these commands are ultimately inserted into a loop to create multiple reports.
I suppose I could use a local that increases with each flexmat command and subsequent loop, or even take the global flexmat_current_loc and increase it for each flexmat table and loop, but I can’t help but think that there is a better, more intuitive way.
Any advice for my predicament?
KevinAttaullah ShahModeratorJanuary 7, 2023 at 1:58 pmPost count: 70
Thanks for bringing this up. I have improved the functionality of flexmat, and as a result, I have updated the asdocx files. To get the updates, use
This thread will be useful to other users of asdocx and flexmat, therefore, I will provide a detailed reply.
appendmatsub-command appends a flexmat file to the current asdocx file (that is is available in the global macro
$active_flexmat_file). Users must use the option
appendmat(). The subcommand is similar to
addmat, but with three main differences:
1. Users do not have to specify the
appendmatautomatically identifies the next available location and appends the flexmat file to it.
2. Files written with flexmat are stored as associative arrays, therefore, they need to be converted back to matrices before appending them to other files. The sub-command
appendmattakes care of this.
addmatsubcommand can be used to add a matrix at any combination of rows and columns, potentially replacing an existing matrix or table. In contrast, the
appendmatcommand will always append the flexmat file to a new location, resulting in the addition of a new table to the file.
Here is a typical use of the sub-command
* Specify the flexmat filename using asdocx asdocx setfile, save(First_Test) * Add contents to the file First_Test flexmat addrow, data(Year, Month, Day) r(1) flexmat addrow, data(2001, March, 21) r(2) * Add notes and title to the table flexmat addparts, title(Table: Test Results for Sample A) /// notes(Sample A was tested for contaminants X, Y, and Z.) * Create another flexmat file and add contents asdocx setfile, save(Second_Test) flexmat addrow, data(Make, Model, mpg) r(1) flexmat addrow, data(Ford, Explorer, 15) r(2) * Add notes and title to the table flexmat addparts, title(Table: Test Results for Sample B) /// notes(Sample B was tested for contaminants A, B, and C) * Create asdocx file and append the earlier two flexmat files to it asdocx, save(REPORT) text(MY REPORT) title replace flexmat appendmat, matname(First_Test) flexmat appendmat, matname(Second_Test) * Export the asdocx file to MS Word asdocx export
* Note on file names
setfileoption of ‘asdocx’ causes ‘flexmat’ to store its files in the
_asdocfolder, located in the user’s current directory. This folder is hidden by default to avoid visual clutter and accidental deletion. In the examples given, the files
Second_Testare stored as
Second_Test.flexmat, respectively, in the hidden directory. Users do not need to specify the folder path or the extension, as flexmat automatically retrieves files from the hidden the directory and adds the
When users are confident that they no longer need to store the asdocx’s working files, they can use:
This will delete all raw files in the “_asdoc” folder.Kevin BlaineParticipantJanuary 9, 2023 at 4:34 pmPost count: 45
Fantastic. Thanks so much for the detailed and quick reply!
– KevinKevin BlaineParticipantJanuary 9, 2023 at 4:43 pmPost count: 45
The changes to the help file makes a huge difference. Tested and works GREAT! Seriously, thank you.Kevin BlaineParticipantJanuary 9, 2023 at 5:03 pmPost count: 45
I think I found a bug:
addparts, title()function used in previous
flexmatcommands/files doesn’t seem to carry over to appended tables when using the `appendmat’ function. The table title is replaced with “Table: Results”Attaullah ShahModeratorJanuary 10, 2023 at 7:39 amPost count: 70
Thank you for your valuable feedback. You can view your contribution in the
flexmathelp file, as I have incorporated your example into the
appendmatsection. In the next update, I will also include your name in the acknowledgement section.
I had originally planned to add titles and notes when using the
appendmatsubcommand at a later time, but your message has provided me with renewed motivation and I will try to find the time to work on it soon.”Attaullah ShahModeratorJanuary 10, 2023 at 2:11 pmPost count: 70
I took some time today to update
addpartssub-command now accurately adds notes and titles to the tables that are appended using the
appendmatcommand. I have updated the provided example to reflect these changes.Kevin BlaineParticipantJanuary 10, 2023 at 4:59 pmPost count: 45
Dr. Shah — THANK YOU! And the acknowledgement is so kind. Really though, you deserve all the credit with this great program!
- You must be logged in to reply to this topic.