Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you assign a string to a variable name?

Status
Not open for further replies.

MelbourneFL

Bioengineer
Sep 17, 2007
2
US
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
 
Replies continue below

Recommended for you

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


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))?
 
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
 
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
 
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
 
>> 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.
 
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?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor