Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X
Hi all,
I have a MathCAD Prime worksheet to calculate a 2-d matrix called HL which itself is indexed with j. I need to calculate this matrix for j:=2..207 and I want each of these matrices to be saved to a comma separated .text file with a different name. I have figured out how, for a given value of j, I can save the file that I want, but I could not figure out how to do all this in a loop. I need a loop, or another way of doing this, so that I can save all those 206 files automatically. Many thanks for your help!
Roman
Solved! Go to Solution.
Hi,
please upload your mcdx file.
Dear Martin,
thanks for your interest to help. I have attached my MathCAD file in my reply to Luc. Does this allow you to offer a solution that would work for me? Many thanks!
Roman
Dear Martin,
many thanks for offering a solution too! I have not tried it yet, but many thanks!
Roman
This little program
Creates n files: The line
M<-matrix...
Creates an n by n matrix with sequential numbers in the rows and columns starting at i
The line
fil <- concat....
Creates a filename containing the number i
And the line
WRITECSV...
Writes out the matrix to the file
Called with:
ït creates 3 matrices, the first contains:
1,2,3
2,2,3
3,3,3
The second contains:
2,3,4
3,3,4
4,4,4
And the third contains:
3,4,5
4,4,5
5,5,5
Success!
Luc
Dear Luc,
thanks for sharing your little program! This is helpful, I learned something already, but I failed to adapt it to my script and make it work. I attach my MathCAD worksheet here. Unfortunately I'm not able to share with the data, but you can see how I calculate the matrix, followed by a manual workaround for printing to file. How would I adapt my script to print each of the i:2..207 matrices to file?
Thanks,
Roman
See if this does the job
Dear Werner,
this works! Thank you so much!
I don't fully understand why it works, but I suspect it has to do with how you defined r as element x, and c element y. I had not seen this used before and don't know what difference it makes to defining r as element 1..8 for example. If you have time to briefly explain or link to an explanation I would be very happy to learn. Oddly, when the script is done, it circles Animal_j in red and says "this value must be a string", but this doesn't matter because the job isdone already.
Kind regards,
Roman
What a range variable like x or y is in the worksheet, is a for loop in a program.
Actually a range can be seen as an implicit for-loop - its not a vector (which often is cause for confusion).
I simply wanted to use the ranges x and y you already had defined and so I used variable names r and c (I was thinking of "row" and "column" to cycle through x and y. Otherwise the program just does what you showed for j=2 and for general j in your sheet.
Without using the already defines worksheet ranges x and y you could use the very same names in the program and use these names as (local) variables in the for-loops:
Now it may be more obvious that these nested for-loops create the very same matrix HL for each value of j as you had shown in the worksheet using the (now obsolete) range variables x and y. These ranges are not needed anymore and can be deleted.
According the error message you get I can only guess and speculate due to the lack of the actual data.
Best guess is that the vector "Animal" has more than 207 elements and that element #208 is not a string like the entries before.
If that's the case, you could use the constant "207" as the last value of the j-loop instead of "last(Animal)" (as can be seen in code snippet above).