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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Translate the entire conversation x

While loop with interactive exit/break

HP_1364456
10-Marble

While loop with interactive exit/break

Hello again
I have (yet another) possibly trivial question.

I want to make a program loop that will add elements to an array as long as the user wants to.

Is this even possible in MathCAD?

In python the basic loop would look something like below (formating difficult):

 

answer='Y'counter=0
array=[]
while answer=='Y':
    answer=str(input("Add another? (Y/n) :"))
    answer=answer.upper()
    array.append(answer)
print(array) 


This can look like this when run in Python:

Add another? (Y/n) : Y
Add another? (Y/n) : Y
Add another? (Y/n) : n
['Y', 'Y', 'N']
ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Werner_E)

Here is on possible approach for my option1, turning the calcs into functions. I followed the example you posted but can't be sure if this approach will fit if the calculations are more complex and forked.

The functions (F1, F2) may have different number of arguments. Prime does not allow a user function which can be called with a varying number of arguments. Way out could be to provide all arguments in a vector and just use the necessary one.

The input data also must not be provided in an matrix, but it makes the automatic creation of the table much easier.

Werner_E_0-1755608393832.png

 

Drawback of this method sure is that arguments and operation which should be done cannot be made selectable via ListBoxes but must be typed in manually by the user which is failure prone. 

Prime 11 sheet attached

 

View solution in original post

24 REPLIES 24
Werner_E
25-Diamond I
(To:HP_1364456)

There is no way that a Mathcad or Prime program can do any user interaction once its running.

Maybe what you are looking for is possible to achieve using one of the advanced controls which is turned into a function, opens a message window and is called from within the Mathcad program.

This should be possible to do in real Mathcad (v. 15 and below) but I doubt that it would be possible to do in Prime as turning an advanced control into a function is not really possible to do and I am not sure if we can call  'MsgBox' from within a Prime script.  Prime's scripted controls are definitely more limited than the controls in old real Mathcad.

Thank you again @Werner_E 

Guess I forgot to mention I am using Prime 11.0.0.0 (Just abandoned MathCAD 15 as our IM infrastructure does no longer supports it. 
Starting to wonder if that was a mistake.. 😢. But we never really used the more advanced features/functions in MathCAD 15, so what I am trying to achieve 

 

I was afraid this would be the answer, as I could not find anything in MathCAD similar to the Python "input" statement.


Basically what I am trying to achieve is to make a "snapshot" of the calculation results (with varying input data) and save this into a table/array/matrix.

I have looked into maybe using the Advanced control "Button", as it appears to run the internal code once every time it is clicked. 
Finding the documentation regarding input/output from advanced controls a bit lacking. Also I have to make a decision about what programming language to use inside the control. I have some general experience using Python, but that seams to require an additional installation, while Jscript and VBscript is available out of the box on Windows?

Anyway, I will make an attempt at the solution provided by @terryhendicott and see how it turns out.

Regards
Hallvard

Hi,

It is possible without programming,

Type the following without the spaces put in for clarity:-

array [colon] [CTRL] M "

Enter first text line between the quotes

If you want another line type, the arrow right takes you outside the quotes:-

[Arrow Right] [Shift] [Enter] "

Enter line of text between the quotes.

Keep going until the array is finished.

Capture.JPG

Correction.

Keys together are [CTRL]+M and [SHIFT]+Enter

 

Hello @terryhendicott 

Thank you for the reply.
What you are explaining is how to manually define an array/vector.

It may be that I could add some text box instructions for the users to achieve this..
Something like
"Add this line to store the result in the table:
inputi:=input  resulti:=result
Where i is the next linenumber"..
May be the simplest solution in the end...

Anyway I realize my explanation was a bit "oversimplified".

In reality the structure of the Prime document is a bit more complex. I will try to outline it below.

 

  1. Area containing global parameters used for calculations (some fixed, and some project specific and changeable)
  2. In this case an "Basic" combo control used to select parameters for the specific case one want to calculate. In this case there are 19 different calculation possibilities, one of which has to be selected (this actually works quite good)
  3. Area containing formulas to calculate the answers
  4. Area containing the actual calculations
  5. (The problem area) The option to save the current input/results into a table/file/somewhere (like using the >> file (append to file) command from a shell.
  6. Change the input (bullet 1, 2 above) to calculate a new case and (optionally) add input and result to the table until all relevant cases have been calculated...

It may be that I have to look into the API or COM interface to achieve this in an interactive manner..

Regards
Hallvard

I notice that the subscript "i" was "unsubscripted" when I posted the last reply (something about illegal HTML code!?

Anyway in the words inputi and resulti the tailing "i" should be subscripted to indicate a vector index.

Regards
Hallvard

Hello again @terryhendicott 

 

Tried to implement a "semi manual" table appending as mentioned earlier.

This is how it looks when I direct the results to the first line in the table (ORIGIN=1 in this document)
first_calc.png

 

And this is how it looks like when I assign it to the second row.

second_calc.png

 

As you can see, the approach fails because the arrays are re-initialized and the previous table values overwritten by 0. ☹️

Regards
Hallvard

 

Hi Hallvard,

Know the question has been answered but would like to let you know how to handle the selection of one calculation from 19 possible combinations with the minimum of fuss..

First I would set up an Excel Spreadsheet that controls management and reporting of production of the worksheets.

Then I would create 19 separate Prime worksheets one for each calculation to simplify input and file saving of actual calculations in a specific project.

I would then set  up hyperlinks in the spreadsheet to the files concerned,

Now all a user needs to do is open the Excel spreadsheet.  Click on the hyperlinks. Answer a few security questions about use of hyperlinks in Excel and the file opens up ready for calculation and saving to actually record the inputs and outputs for the specific project.

 Capture2.JPG

Capture3.JPG

Capture4.jpg

Capture5.JPG

Capture6.JPG

Cheers

Terry

Hello Terry

Thank you for helping.

I am not sure this is what I was trying to do though.
(See my other post where I compare the problem to going to a restaurant)

The “end result” I am trying to achieve is a worksheet which documents the calculations, formulas (i.e. contains MathCAD/Prime statements which can be recognized, even to non-MathCAD/Prime users) and has a table at the end which shows which calculations have been selected and the value of the relevant input parameters and the calculation results.

I think maybe the “WRITEEXCEL” and “READEXCEL” functions can be used to add calculations to a table which in the end can be read and displayed as part of the worksheet.

You have given me something to think about.

Actually what I am working on now is how to make a counter which is increasing every time a menu selection (Advanced control) is selected as used in the “Bulls and Cows” game mentioned by @LucMeekes). This seems to be the common issue no matter how I try to build the result matrix.

Regards
Hallvard

Werner_E
25-Diamond I
(To:HP_1364456)

Its still not absolutely clear to me what exactly you try to achieve even though in the meantime you outlined your worksheet structure, but it gets clearer.

Once you change input values at the top of the sheet, the sheet is recalculated from anew and values which were calculated before are lost.

I can see two options to overcome this:

 

1) turn your calculation into functions of the input values. Then you can call these function(s) one after the other using varying inputs and collect all results in a suitable data structure like an array or nested matrices.

 

2) Save the results of one calculation run in an external file using an appropriate WRITExxx  function.
To collect a series of runs and avoid to overwrite the data already saved you could either

a) use automatically created different file names for each run, so you end up if one file for each run

b) Use the appropriate READxxx function to read in the data already saved, combine it with the new data and resave

c) If possible an appropriate you also can use WRITEPRN the first time and then APPENDPRN to add data to an already existing file

Which function for writing data is most appropriate depends on the type and structure of your data. See here what Prime has to offer.

 

Hello again @Werner_E 

Thank you for your continued support. 😁

Basically what I want is to do is to allow the user to

  1. Define the input
  2. Calculate the result
  3. Add the input and result to a table
    (well it must be a matrix as far as I understand, but something looking like a table can be made by stacking and augmenting arrays/vectors)
  4. Ask the user if he/she wants another calculation if Yes goto 1 else continue to 5
  5. Display the result which contains a summary of all the calculations.

I may add that I am attempting to put this into a "Template", so I do not know in advance exactly which calculations are needed on the specific project. Could possible include instructions to the users on how to prepare the input of all calculations in one go..

I guess I could also use a similar approach as in the previous case you helped me with where I basically performed all possible calculations, and then used check-boxes to select which results to include in the result table. (Many more possible combinations in this case, but CPU cycles are cheap these days.) 😉

But from what you are writing it may look like saving and reading to/from a file may be another possible solution.
I have not investigated the file related functionality yet..

Regards
Hallvard

LucMeekes
23-Emerald IV
(To:HP_1364456)

The challenging part is your step 4, described as that (the) Mathcad (sheet) takes the initiative to interact with the user.

You may have a look at what was done to create games using Prime, here: https://community.ptc.com/t5/Mathcad/Mathcad-Community-Challenge-Summer-2024-Game-Gallery/m-p/959091 

 

Success!
Luc 

Hello @LucMeekes

Yes, the interactivity is the challenging issue in my view too.
(And the reason I want interactivity is to make it easier for end-users with limited MathCAD knowledge)

I had a look at the "Mastermind simulation" (Bulls and Cows) video, and had the "log" (right part of the document) been updated automatically I would see that as an indication that this game had found a workaround (the document would grow automatically for each attempt being made).

I see the counter is updated though.. If it could be used as an index to an array.. Maybe if I predefine an array and only change the values based on the counter...

Regards
Hallvard

Hello again @LucMeekes 

I guess what I cannot quite wrap my mind around is the fact that the entire document is recalculated from top to bottom each time a change is made.  So the idea I had in the previous post does not work. (I can change an element in a predefined array, but I cannot simply change a second element by changing the index in the same statement, because the whole array will be redefined to its initial value.
Mathcad 2025-08-20 100204.png.)

Regards
Hallvard

LucMeekes
23-Emerald IV
(To:HP_1364456)

Maybe these two undocumented functions may be of help:

LucMeekes_0-1755684513080.png

Success!
Luc

Thank you @LucMeekes 
This looks promising. It looks like it is even possible to use it to append to an array.
Value gets more and more elementsValue gets more and more elements
Thank you again, I have to experiment to see how it works together with my combo-box.
Wonder why it is undocumented, can it suddenly disappear in a future release?

Regards
Hallvard

Werner_E
25-Diamond I
(To:Werner_E)

Here is on possible approach for my option1, turning the calcs into functions. I followed the example you posted but can't be sure if this approach will fit if the calculations are more complex and forked.

The functions (F1, F2) may have different number of arguments. Prime does not allow a user function which can be called with a varying number of arguments. Way out could be to provide all arguments in a vector and just use the necessary one.

The input data also must not be provided in an matrix, but it makes the automatic creation of the table much easier.

Werner_E_0-1755608393832.png

 

Drawback of this method sure is that arguments and operation which should be done cannot be made selectable via ListBoxes but must be typed in manually by the user which is failure prone. 

Prime 11 sheet attached

 

Hello again @Werner_E 

In principle this document looks very much like the structure of my document (BTW, what is the proper name of a MathCAD (Prime) document?)

The difference being the "inputList" definition where I have a "Basic Combo box". This allows me to select several input parameters in one go, without editing the document (just the selection in the "Basic Combo box"). But it only allows one selection at a time, and I want the document to contain a table summarizing several different "load cases".

But I am quite sure your solution will work, the issues I have with this:

  • The user has to manually set up the "inputList" rather than selecting it from a "Basic Combo box",
    This increases the possibility of user error.
  • The user needs to know in advance which load cases and input values to use

Anyway I will use the suggested approach.

Regards
Hallvard

 

 

Werner_E
25-Diamond I
(To:HP_1364456)


  • The user needs to know in advance which load cases and input values to use


Not necessarily. If a user wants to add a load case, instead of creating a new inputList (s)he just adds a line in the existing one.

Of course the whole sheet would be calculated from anew and the load cases which were already calculated are calculated again. I see that this could be annoying if the calculations are very time consuming. In this case the approach of using an external file may be more appropriate.

Problems to solve with this approach are

.) you don't know the directory structure of the user and so probably you must have the user to manually type in a path string (not sure if Primes scriptable components are capable enough to make a file path selectable via a file selection window).

.) You have to indicate in some way that the game starts. The fist time you have to use WRITEPRN, otherwise APPENDPRN

.) When the user finished the calculations, the file would still remain sitting on his storage media and should be deleted. Not sure if a scripted component could be used to do this from within the Prime sheet. Otherwise you would have to instruct the user to clean up manually.

 

And because you mentioned in a reply that you are considering predefining a larger matrix (which does not work because values that have already been entered do not survive a recalculation): If you can specify a maximum number of load cases, you could copy the input section with the combo boxes several times. They should have a default value of something like “none” or NaN for at least one of them so you later decide if its a valid load case which should be calculated or just a dummy. The user then selects one set of load cases after another. The individual input sections could be arranged in collapsed areas that the user opens as needed for input.
After all input sections, your program can then calculate all relevant valid load cases and display them in a table.
Of course, this approach means that after entering the second load case, the first one is calculated again, and so on.

Hello again @Werner_E 

 

Regarding "knowing in advance"..

This actually is a bit like going to a restaurant. 

  • Upon arrival you are presented with a menu
  • You pick your choices from the menu (you can pick one or several items from the menu)
  • Also the waiter may ask some general preferences not mentioned in the menu 
  • During / after the meal you may decide to get an additional drink or maybe a desert
  • When picking up the check everything you have ordered is listed, you may also add a tip.

You usually do not know before seeing the menu what you are going to have (unless you are a are "regular" at the restaurant in question  😉 ). Most people going to a restaurant to eat do not bring a list of what they are going to order (which in this case is analogous to the "inputList".)

However this is not a restaurant and editing the inputList is definitely a possible solution, that I have started working on.

Have not explored the file handling possibilities yet and I also see that @terryhendicott is suggesting going via Excel files. Both these options I will need to explore further. However it is also a goal to present the results in a clean looking (single file) report which can be sent for approval to a Ship classification society. And MathCADs ability to render mathematical expressions must be utilized in the report to document the calculation.

Regards
Hallvard

Hi Hallvard,

 

In version 12 of Prime to be released, one of the predicted new features, will be I hope, page numbering starting at other than one.

I hope someone from PTC is watching this.

This means you can chain print each of the say 19 Worksheets with running page numbers as part of a final report.

The report production can also be included in the overall control Excel spreadsheet.

 

In the oil and gas industry at the start of each stage of project generation a complete list of predicted deliverables is made by senior staff in advance of any work on the deliverables.  This way production of the deliverables can be managed.  The list is never complete or static as changes can be made during the project stage.  This is subject to a formal change management process.  Part of which is to amend the list of deliverables as necessary to keep the design under control and on track. Many hundreds of deliverables are handled by each of the many disciplines. 

 

I recommend the Excel method.  There are software packages that automate the deliverable creation and all the inter-discipline checking needed of them.

 

Cheers

Terry  

LucMeekes
23-Emerald IV
(To:HP_1364456)

"(BTW, what is the proper name of a MathCAD (Prime) document?)"

I'd say "A Mathcad (or Prime) sheet" or "worksheet", but never 'spreadsheet'.

 

Success!
Luc

It's worksheet.

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.

Thank you @LucMeekes and @DJNewman 

Then Worksheet it is! 🙂

Regards
Hallvard

Announcements

Top Tags