cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community email notifications are disrupted. While we are working to resolve, please check on your favorite boards regularly to keep up with your conversations and new topics.

Writing to file in for loop in MathCAD Prime 9.0

RA_10945383
3-Visitor

Writing to file in for loop in MathCAD Prime 9.0

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

1 ACCEPTED SOLUTION

Accepted Solutions

See if this does the job

Werner_E_0-1717439313211.png

 

View solution in original post

9 REPLIES 9

Hi,

please upload your mcdx file.


Martin Hanák

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

MartinHanak_0-1717436959851.png

MartinHanak_1-1717437042471.png

 


Martin Hanák

Dear Martin,

many thanks for offering a solution too! I have not tried it yet, but many thanks!

Roman

LucMeekes
23-Emerald III
(To:RA_10945383)

This little program

LucMeekes_0-1717177348777.png

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:

LucMeekes_1-1717177597992.png

ï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

Werner_E_0-1717439313211.png

 

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:

Werner_E_0-1717493141252.png

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).

 

Top Tags