Skip to main content
3-Newcomer
May 31, 2024
Solved

Writing to file in for loop in MathCAD Prime 9.0

  • May 31, 2024
  • 2 replies
  • 2511 views

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

Best answer by Werner_E

See if this does the job

Werner_E_0-1717439313211.png

 

2 replies

24-Ruby III
May 31, 2024

Hi,

please upload your mcdx file.

3-Newcomer
June 3, 2024

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

24-Ruby III
June 3, 2024

MartinHanak_0-1717436959851.png

MartinHanak_1-1717437042471.png

 

23-Emerald IV
May 31, 2024

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

3-Newcomer
June 3, 2024

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

Werner_E25-Diamond IAnswer
25-Diamond I
June 3, 2024

See if this does the job

Werner_E_0-1717439313211.png