×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Can you assign a string to a variable name?

Can you assign a string to a variable name?

Can you assign a string to a variable name?

(OP)
Hello guys,

I'm a rather experienced user of Mathcad but I'm stuck with the string functionality of Mathcad. What I can do is creating a string that is a variable name via loop, e.g. data_point_0 via:

string:=concat("data_point_","i").

The result is then (if i=0):

string=data_point_0

The problem for me is that in the next step I want to use this string to point to the variable "data_point_0" (that is created somewhere earlier in my program)...something like this:

result:=$string

I want to assign the value of "data_point_0" to "result". But I don't know how to do this. In other program languages you usually use the "$" sign to indicate that what follows is a string but how do you do it in Mathcad?

Thanks for any help,

Alexander

RE: Can you assign a string to a variable name?

The simple answer is that can't - or, at least, not without being 'clever'.  There is no direct way of doing what you want; Mathcad doesn't have the capability to create variable names from strings (or an 'eval' equivalent).

It can be done in several ways, the easiest possibly being to use a component and some scripting to look for the last value of data_point_0, but there are some caveats associated with it.

Probably your best bet, as this forum doesn't appear to allow worksheets to be attached, is to post your question on the main Mathcad forum at

http://collab.mathsoft.com/~Mathcad2000

As an aside, I'm not sure how you get

-- data_point_0

from

-- string:=concat("data_point_","i").
-- The result is then (if i=0):

concat should return data_point_i

to get what you want, I would have thought you'd need to write:

string:=concat("data_point_",num2str(i))?

RE: Can you assign a string to a variable name?

How about making data_point a vector and using your loop to access the elements.

data_point[0:= ...
data_point[1:= ...

Then you can use a variable index to access the elements of data_point.

result:=data_point[i

Peter

RE: Can you assign a string to a variable name?

(OP)
Thanks guys. Although I don't like your answer...  :(

@excognito: Of course you are right with the error in my initial post. I did it from memory and forgot the num2str-command. I'll be checking out the forum you suggested.

@pstuckey: That isn't working in my case since the number of data points changes every time I use the program for some other data (which will be quite often). So I would have to change the program every time...that is ok...but it is not nice. Besides each data point is actually a vector what makes things more difficult.

Alexander

RE: Can you assign a string to a variable name?

Not necessarily.  You can use Rows() and Col() to determine the size of the array and set the array indices.  There really should be no need to individually name you variables.

TTFN

FAQ731-376: Eng-Tips.com Forum Policies

RE: Can you assign a string to a variable name?

>> That isn't working in my case since the number of data points changes every time I use the program for some other data (which will be quite often). So I would have to change the program every time...that is ok...but it is not nice. Besides each data point is actually a vector what makes things more difficult.

As IRStuff points out, the variability in size should not be an issue.  In addition, Mathcad can handle nested arrays, which allows you to create a vector of vectors.  Alternatively, you could augment the vectors into 2D matrix and access them by columns - provided each data point is of the same length or you can handle the extra zeros that Mathcad will add to vectors to make them all the same length as the longest one.

RE: Can you assign a string to a variable name?

I was wondering if you could define two vectors that have matched indices?  So the first vector would contain the names of the data "points"  e.g.
i:= 0,1 ; rows(data)-1
datapoint_i:=concat(data_point_",num2str(i))  
and the second vector would have each element be a vector of numbers
e.g. Data_i:= submatrix(DataMatrix,i,i,0,cols(Datamatrix)-1)(each row being the data from each sample, I presume).  

As your process your numeric data using some kind of a programming loop, the condition where you determine which data vector is the right answer will generate the index into the matched arrays and then you can can use that index to reference the name of the associated data point (e.g. datapoint_i) out as the correct answer.

Would this meet your needs?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources