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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Matrix manipulation MathCad

Hans_Westerweel
10-Marble

Matrix manipulation MathCad

Hi,

 

I have some questions about the attached file (questions are highlighted yellow on page 3 & 4). I'm trying to extract columns from my matrices Pz, Px, and My and combine them into new matrices, each with a Pz, Px, and My column, plus zero values for Py, Mx, and Mz.

 

My first question: why do I lose my units when I convert the nested matrices Pz, Px, and My to regular matrices with the method I used?

 

Second question - in order from easy to difficult: How do I create one matrix taking the first column from Pz, Px, and My? How to create three matrices with all columns extracted one by one? How to keep the number of matrices flexible in case columns in Px, Pz, and My are removed or added? I'd really like to understand how to do this for future projects as well.

 

Below an image from Excel on what I'm trying to achieve.

Hans_Westerweel_0-1698240799142.png

Thank you!

 

 

 

ACCEPTED SOLUTION

Accepted Solutions

Your function Pz (as well as the others you defined) should not have the sane name as the variable you would like to use as its argument.

Furthermore is your function not using the argument M for its calculation but it uses the worksheet variable Pz and immediately overwrites the argument M with its calculations.

So if you really want to use a function, you should do it that way (I am using the name "R" for the local variable because I am thinking on "Return value" or "Result"):

Werner_E_0-1698245345091.png

 

But if your goal simply is to unravel the nested vector Pz, the most straightforward way would be

Werner_E_1-1698245495711.png

 

You can also avoid the nested structure right away by using the "stack" command

Werner_E_2-1698245669259.png

 

 

View solution in original post

12 REPLIES 12

Your function Pz (as well as the others you defined) should not have the sane name as the variable you would like to use as its argument.

Furthermore is your function not using the argument M for its calculation but it uses the worksheet variable Pz and immediately overwrites the argument M with its calculations.

So if you really want to use a function, you should do it that way (I am using the name "R" for the local variable because I am thinking on "Return value" or "Result"):

Werner_E_0-1698245345091.png

 

But if your goal simply is to unravel the nested vector Pz, the most straightforward way would be

Werner_E_1-1698245495711.png

 

You can also avoid the nested structure right away by using the "stack" command

Werner_E_2-1698245669259.png

 

 



Second question - in order from easy to difficult:

How do I create one matrix taking the first column from Pz, Px, and My?

 

 


Werner_E_3-1698246377721.png

Unfortunately you can't change the units when displaying a matrix with mixed units.

If your intention is the export to Excel, you would make it unitless anyway:

Werner_E_4-1698246526495.png

 


How to create three matrices with all columns extracted one by one?
How to keep the number of matrices flexible in case columns in Px, Pz, and My are removed or added?

 

 


Maybe that way:

Werner_E_0-1698247473991.png

 

 

According the zero values for Py, Mx and Mz you could use

Werner_E_1-1698248193267.png

 

That looks very much like what I wanted to do. I don't have Prime 9 yet, is there a way to post your sheet in Prime 8 version? Thank you.

 

And yes, the intent is to export to either Excel or Notepad, so the units do not matter.

Unfortunately provides PTC no way to save or convert a sheet to the format of a lower version.

But actually you see in the pics all you need to retype the few lines.

Furthermore, which is not shown in the pics, I unraveled the three nested matrices Px, Pz and My as I had shown with Pz in a prior reply like this

Werner_E_0-1698251767343.png

I used this method because it was easier to do compared to using the stack command right away 😉

 

To actually see all three matrices when the nested matrix M^T is displayed, you have to turn off the option to collapse nested matrices

Werner_E_1-1698252090448.png

 

To create a separate Excel file for every matrix in M, you could use something like

Werner_E_2-1698253585878.png

The assignment to dummy variables is necessary to "trigger" the write command(s).

The Excel files are numbered starting with #1 (independent from the setting of ORIGIN) and look like this

Werner_E_3-1698253747631.png

(over here we use the comma as decimal separator)

 

 

 

 

I was able to unravel the nested matrices with the method you showed. When I use a function to unravel them, I get an error message "this variable is undefined". Do you know why that's happening? Just for my knowledge.

Hans_Westerweel_0-1698255115977.png

 

Remove the inline evaluation (The "= ? klf") from the definition of the Pz_f function and it should work.

As a guidance: Avoid using inline evaluation with Mathcad / Prime. Often it works, ever so often it causes problems.

 

Success!
Luc

As Luc already wrote its the inline evaluation, the "=" after the definition of the function Pz_f().
You would get the very same error if you define " f(x):=sin(x)= ". How should Prime know for which value of x you would like the function to be evaluated?

And I second what Luc said that its usually a good idea to avoid inline evaluations - even if they are used in a legitimate way (as the one at the end of your screen shot.

Thats the reason why I usually use separate regions for the definition and the display of a variable, like in

Werner_E_0-1698257968530.png

BTW, I don't think that you need a function like Pz_f() anyway. All thats needed is what is shown here above and thats basically exactly what you did. just not assigned as a function but rather as the new unraveled Pz. I assumed that you won't need the nested structure of the original Pz anymore and so had not chosen a new variable name.

 

That makes sense, yes. Thanks for all the explanations so far, you guys are amazing resources! 
I'll try and work out the Excel file program later today to see if I can make that work.

I remember you can also have MathCad write a file to Notepad.

Does that work in a similar way as the method below? 

 

Hans_Westerweel_0-1698258592242.png

 


@Hans_Westerweel wrote:

That makes sense, yes. Thanks for all the explanations so far, you guys are amazing resources! 
I'll try and work out the Excel file program later today to see if I can make that work.

I remember you can also have MathCad write a file to Notepad.

Does that work in a similar way as the method below? 

 

Hans_Westerweel_0-1698258592242.png

 


You may try WRITETEXT(), WRITECSV()  or WRITEFILE() instead of WRITEEXCEL(). 

Look them up in the help for additional arguments you may add.
WRITEPRN() creates an ASCII file, too, but with an added proprietary header, so you probably would have to edit and post-process the file in Notepad.

Unfortunately the header texts will appear in quotes "" in the created ASCII file.

To avoid this and/or to create an ASCII file with a special format to be used as input in another program, it would be necessary to write a elaborate custom output routine in Prime which basically would use WRITEBIN() to write the data.

 

Thanks again for the elaborate tips! This stuff is going to be so helpful for my job.

I'm having one last issue (almost there). When I export to a text file, some of the columns shift because the output isn't rounded.

 

Hans_Westerweel_0-1698326788838.png

 

I tried rounding it like this, but it says my variable Pz_ is undefined. I feel like I'm not understanding something very basic here about MathCad programming.

 

Hans_Westerweel_1-1698327009430.png

 

 

Three errors:

  1. The loops for i an j should run from 0 to rows -1 resp col-1. If the matrix has 10 rows, the row index runs from 0 to 9 (and not 10)
  2. When you typed Pz_ in the round function, the underline got accidentally subscripted and thats a different (undefined) variable, hence the error message
  3. You used the wrong unit in the round function, it should be klf, not kip!

Once these errors are corrected, it works as expected

Werner_E_0-1698330093301.png

 

But there is an easier way to achieve the same result using the vectorization operator:

Werner_E_1-1698330154399.png

 

The latter could also be used in the WRITExxx() command, so you can use the exact values in the calculations in the Prime sheet and do not need to define a separate variable just for the export.

For the unit-less export you could also use

Werner_E_2-1698330564475.png

Note the lower case "r" in "round".

Otherwise you can of course still use "Round" with the capital "R"

Werner_E_3-1698330619366.png

 

 

Announcements

Top Tags