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?
-
AuthorPosts
-
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?
Thanks,
KevinThanks 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
asdocx_update
This thread will be useful to other users of asdocx and flexmat, therefore, I will provide a detailed reply.
Theappendmat
sub-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 optionmatname()
when usingappendmat()
. The subcommand is similar toaddmat
, but with three main differences:1. Users do not have to specify the
location()
option asappendmat
automatically 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
appendmat
takes care of this.3. The
addmat
subcommand can be used to add a matrix at any combination of rows and columns, potentially replacing an existing matrix or table. In contrast, theappendmat
command 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
appendmat
.* 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
Thesetfile
option of ‘asdocx’ causes ‘flexmat’ to store its files in the_asdoc
folder, 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 filesFirst_Test
andSecond_Test
are stored asFirst_Test.flexmat
andSecond_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.flexmat
extension.When users are confident that they no longer need to store the asdocx’s working files, they can use:
asdocx clear
This will delete all raw files in the “_asdoc” folder.
Fantastic. Thanks so much for the detailed and quick reply!
– Kevin
The changes to the help file makes a huge difference. Tested and works GREAT! Seriously, thank you.
I think I found a bug:
The
addparts, title()
function used in previousflexmat
commands/files doesn’t seem to carry over to appended tables when using the `appendmat’ function. The table title is replaced with “Table: Results”Thank you for your valuable feedback. You can view your contribution in the
flexmat
help file, as I have incorporated your example into theappendmat
section. 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
appendmat
subcommand 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.”I took some time today to update
flexmat
. Theaddparts
sub-command now accurately adds notes and titles to the tables that are appended using theappendmat
command. I have updated the provided example to reflect these changes.Dr. Shah — THANK YOU! And the acknowledgement is so kind. Really though, you deserve all the credit with this great program!
EDIT: Fixed! I was separating the
addparts, notes
andaddparts, title
into two commands. Putting both under the sameflexmat addparts
command fixed it. -
AuthorPosts
- You must be logged in to reply to this topic.