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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Help turning textbox inputs into matrix

ptc-5105545
1-Visitor

Help turning textbox inputs into matrix

Hello,

I am trying to create a matrix of numbers from a textbox and having a little trouble doing it. I would appreciate any help I can get. I have attached a sheet with my problem.

Thank you!

ACCEPTED SOLUTION

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

I wonder why you would go through all the hassle of using a scripted component and converting its output to a vector instead of inserting the vector directly or via table input. Its more complicated and far more failure prone in my opinion.

Best approach possibly would be to let the VBasic script do the parsing and return of a vector.

Nevertheless find attached yet another solution using Mathcads programming facilities.

BTW, its not adviseable to mark the question as "assumed answered" from the very beginning as people might overlook it that way.

View solution in original post

35 REPLIES 35

See the attach please

Valery beat me to it, but here's an alternative version.

Alan

Werner_E
25-Diamond I
(To:ptc-5105545)

I wonder why you would go through all the hassle of using a scripted component and converting its output to a vector instead of inserting the vector directly or via table input. Its more complicated and far more failure prone in my opinion.

Best approach possibly would be to let the VBasic script do the parsing and return of a vector.

Nevertheless find attached yet another solution using Mathcads programming facilities.

BTW, its not adviseable to mark the question as "assumed answered" from the very beginning as people might overlook it that way.

RichardJ
19-Tanzanite
(To:Werner_E)

Best approach possibly would be to let the VBasic script do the parsing and return of a vector.

Yes. This requires very little code in VBscript.

Werner_E
25-Diamond I
(To:RichardJ)

There is a problem with localized Windows versions like the German one I am

working with - the decimal point is ignored here (our versions use the comma

instead of the decimal point.

I cannot insert a pic at the moment (don't know why it doesn't work) but I get "54" instead of "5.4".

RichardJ
19-Tanzanite
(To:Werner_E)

I don't see any good solution to that. VBscript assumes numbers will be typed in local format, but Mathcad doesn't. The text box could be made to work if the numbers were typed as 1;5,4;6, but that's inot consistent with the number format in Mathcad.

Werner_E
25-Diamond I
(To:RichardJ)

Richard Jackson wrote:

I don't see any good solution to that. VBscript assumes numbers will be typed in local format, but Mathcad doesn't. The text box could be made to work if the numbers were typed as 1;5,4;6, but that's inot consistent with the number format in Mathcad.

If there is a way to determine the decimal point character of the windows system (guess there are only "." and "," around) from a VBscript a way would be to substitute after splitting the text at the commas every point for this character in the varaibles StringVec. Not sure how this could be accomplished, though.

Werner_E
25-Diamond I
(To:RichardJ)

Here is the implementation of a very bold method to determine if Windows expects a decimal point or comma.

Hope it works for every localized version of Win.

The method is not very elegant, I know, and it uses the effect, that in my German Windows CDbl("1.0") does not throw an error but results in number 10.

---------------

Dim Vec()

Sub TextBoxEvent_Exec(Inputs,Outputs)

StringVec = Split(TextBox.Text, ",")

ReDim Vec(UBound(StringVec))

For i = 0 To UBound(StringVec)

If CDbl("1.0") = 10 Then

StringVec(i) = Replace(StringVec(i),".",",")

End If

Vec(i) = CDbl(StringVec(i))

Next

Outputs(0).Value = Vec

End Sub

RichardJ
19-Tanzanite
(To:Werner_E)

I can't figure any good way to handle this inside the script. I even looked on some developer forums. This issue comes up regularly as a question, but nobody seems to have a good answer. In Mathcad I think the best solution is to split the work between the script (which is then extremely simple) and Mathcad (the str2num function).

Werner_E
25-Diamond I
(To:RichardJ)

Richard Jackson wrote:

I can't figure any good way to handle this inside the script.

Why? Whats wrong with the way to handle it I posted here: http://communities.ptc.com/message/209260#209260 ?

The trick with the 1.0 getting 10 may not work in all versions, according to Vladimirs post, but the last way of letting the script convert 1/2 to a string and looking which decimal character it uses, should work with every Windows version, I guess (Google was my friend, here ;-). If one does not insist on the comma being the number separator we could change the script easily (adding an additional Replace line) to accept both possible decimal characters.

RichardJ
19-Tanzanite
(To:Werner_E)

Why? Whats wrong with the way to handle it I posted here: http://communities.ptc.com/message/209260#209260 ?

What's wrong with it is that I didn't see it

Werner_E
25-Diamond I
(To:RichardJ)

What's wrong with it is that I didn't see it

Thats admittedly a huge drawback of this method 😉

But in fact in this system threads are going to be confusing and unclear very quick. And there is nothiong we can do against it as we do not have a treeview as in the old collab or the possibility to collaps (sub)threads or sort a thread by date disregarding which post a posting is the reply to.

RichardJ
19-Tanzanite
(To:Werner_E)

That's the thing I dislike most about these new forums. When the thread gets long it gets hard to follow, and if you miss one post the chances of noticing that later are close to zero. New is not always better.

Werner_E
25-Diamond I
(To:RichardJ)

Richard Jackson wrote:

That's the thing I dislike most about these new forums. When the thread gets long it gets hard to follow, and if you miss one post the chances of noticing that later are close to zero. New is not always better.

Fully agreed. The treeview and the unread flag in combination with filtering for unread postings only was a simple but yet powerful instrument. Its a shame we cannot have these in a new trendy system.

Werner Exinger wrote:

There is a problem with localized Windows versions like the German one I am

working with - the decimal point is ignored here (our versions use the comma

instead of the decimal point.

You can use a dot or a comma as decimal separator in my WebSheets:

Dot-Comma.png

As far as I know, you cannot look at Websheets to see how its done, so its not that helpful, I guess.

If I know that my sheet is run on a system which uses the decimal comma its would be easy to use VBscripts "replace" command to change every decimal point in a string to a comma. The number given to Mathcad will be interpreted correctly that way.

But the question is, how to write a sheet/script which will work equally well on any localized version of Windows. In other words, how to determine which decimal comma character is used on the system the script is running on.

One way I could think of, is to convert the string "1.5" into a number using CDbl() and compare the result to 1.5 (or 15). But thats a bit bold, there has to be a more "legal" elegant way.

Werner Exinger wrote:

As far as I know, you cannot look at Websheets to see how its done, so its not that helpful, I guess.

I hope the idea is helpful. No problem to write his add function. I (a teacher!) can sent it. But I think more interesting to do it yourself.

I (a teacher!) can sent it.

Too late 😉 --> http://communities.ptc.com/message/209247#209247

Werner Exinger wrote:

I (a teacher!) can sent it.

Too late 😉 --> http://communities.ptc.com/message/209247#209247

I use not VBScript - I use an user Mathcad-function.

A Mathcad sheet with any Script is... Virus may be...

Valery Ochkov wrote:

Werner Exinger wrote:

I (a teacher!) can sent it.

Too late 😉 --> http://communities.ptc.com/message/209247#209247

I use not VBScript - I use an user Mathcad-function.

A Mathcad sheet with any Script is... Virus may be...

Thats true, but as its being said: "No guts, no glory!". Thats well to say if you are working in a VM in such cases.

RichardJ
19-Tanzanite
(To:Werner_E)

I don't think a script could be a virus. There is no way for it to self propagate. It certainly could be malware though. There's a very old thread somewhere in which I proposed a certificate system so that scripts from trusted authors would not bring up a warning message when the worksheet was opened. I have also brought up this idea with PTC for implementation in Prime, but I have no idea where it stands.

Werner_E
25-Diamond I
(To:RichardJ)

Richard Jackson wrote:

I don't think a script could be a virus.

Virus, worm, malware - maybe just a matter of definition. What remains is, that Valery is right in that scripted components represent a potential risk and if you can achieve the same or a similar effect using Mathcad routines (and maybe the scriptless web components) its preferrable to do so.

Some kind of cerification system may be of help, a configurable script behaviour based warning system maybe even more. But I don't expect to see any of these implemented in the near future.

BTW, couldn't a script look for other Mathcad files on the disk and implement itself there - that way propagating itself?

RichardJ
19-Tanzanite
(To:Werner_E)

But I don't expect to see any of these implemented in the near future.

I don't expect to see them even in the distant future

BTW, couldn't a script look for other Mathcad files on the disk and implement itself there - that way propagating itself?

I guess, in theory, if it's a .xmcd file the file is plain text so it could modify the file (there is no way to read or write binary files in VBscript). And, in theory, it would also be possible to write a Mathcad worksheet with no scripted component in it that modified another worksheet to add such a component. A scripted component is not represented by simple XML code though, so doing it in practice would be difficult. It's hard for me to imagine anyone putting in the large amount effort required when the number of possible targets is so small.

Richard,

I'm having the following error when trying to open your worksheet (in Mathcad 15 M020):

Pic_1.PNG

Pic_2.PNG

Werner_E
25-Diamond I
(To:VladimirN)

Maybe it would be helpful to translate the error message.

It looks like CDbl throws the error.

In my German Windows Alans sheet works, but I get 54 instead of 5.4.

Could it be that the "." throws an error in Russian Windows while it is ignored in my German one?

What about the sheet here http://communities.ptc.com/message/209247#209247

Do you encounter the same error there?

RichardJ
19-Tanzanite
(To:Werner_E)

I think Werner is almost certainly right. It's probably an issue with the regional settings.

Werner Exinger wrote:

...Maybe it would be helpful to translate the error message.

It looks like CDbl throws the error...

1-st error: "Results in the call COM component returned an error in the format of HRESULT E_FAIL".

2-nd error:

Runtime error Microsoft VBScript

Line:12 Error:0

Type mismatch: 'CDbl'

Werner Exinger wrote:

... What about the sheet here http://communities.ptc.com/message/209247#209247

Do you encounter the same error there?...

I have the same error in "help_2.xmcdz" worksheet. Our regional settings (Russia and Austria) for the decimal part is used a comma ",".

Werner_E
25-Diamond I
(To:VladimirN)

So it seems that even as German and Russian Windows use the comma as decimal character, the behaviour is different if a colon is in the number. "5.4" throws an error in the Russian version but results in number 54 in the German one ( I expected an error myself and was surprised about the outcome). I guess this is because when we chose German format we automatically get the colon as group separator between groups of three digits while in the Russion scheme there is a simple space.

German.png

Werner_E
25-Diamond I
(To:VladimirN)

Here is a modified worksheet where I detect the decimal character in a different way.

If the foregoing throws an error for you, chances are that this one works.

----------------------

Sub TextBoxEvent_Exec(Inputs,Outputs)

DecChar = Mid(CStr(CDbl(1/2)), 2, 1)

StringVec = Split(TextBox.Text, ",")

ReDim Vec(UBound(StringVec))

For i = 0 To UBound(StringVec)

StringVec(i) = Replace(StringVec(i),".",DecChar)

Vec(i) = CDbl(StringVec(i))

Next

Outputs(0).Value = Vec

End Sub

Announcements

Top Tags