Thread Subject: Using a saved string as a variable name?

Subject: Using a saved string as a variable name?

From: John

Date: 21 Mar, 2010 02:21:05

Message: 1 of 7

hi,

I would like to use a saved string as a variable name for a structure, for example,

I want the user to input the variable's name, this string then gets saved to a variable - let's name this variable 'userInput'. I then need to use the string stored in this variable as the root name for a structure. e.g. 'userInput'.data, where 'userInput' is replaced with the string stored in the variable.

I was wondering if this was even possible.

John

Subject: Using a saved string as a variable name?

From: ImageAnalyst

Date: 21 Mar, 2010 02:30:39

Message: 2 of 7

John:
I believe that's related to section 4.6 of the FAQ.
http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
It's generally frowned upon. However there have been some ways
presented in this newsgroup where the FIELD name of the structure
(rather than the root name of the structure) can take on such a
variable field name. Not sure of the links - you'd have to do a
search of the newsgroup.

What's your use case for this? Why would the user care what variable
names are used internally in the program? Just do the task and
present the results back to the user - the user shouldn't care what
happens inside.

Subject: Using a saved string as a variable name?

From: Rune Allnor

Date: 21 Mar, 2010 07:41:23

Message: 3 of 7

On 21 Mar, 03:21, "John " <jsun6...@gmail.com> wrote:
> hi,
>
> I would like to use a saved string as a variable name for a structure, for example,
>
> I want the user to input the variable's name, this string then gets saved to a variable - let's name this variable 'userInput'. I then need to use the string stored in this variable as the root name for a structure. e.g. 'userInput'.data, where 'userInput' is replaced with the string stored in the variable.
>
> I was wondering if this was even possible.

It is possible, but a very bad idea. You want variable names
that are known - and determined - by *you*, the programmer,
not the dynamics of the user situation.

How would you know what variable to use downstream?

Instead, use a struct with whatever field s you think are
needed / useful, and search the relevant fields when you
need to.

Rune

Subject: Using a saved string as a variable name?

From: Bruno Luong

Date: 21 Mar, 2010 09:11:02

Message: 4 of 7

"John " <jsun6383@gmail.com> wrote in message <ho3vqg$4re$1@fred.mathworks.com>...
>
> I was wondering if this was even possible.
>
> John

Yes, there is a FEX file for this purpose:
http://www.mathworks.com/matlabcentral/fileexchange/23078-workspace

>> ws=workspace; % called only once
workspace 'caller' (ws) usage:
ws.(name)(...) = value;
lhs = ws.(name)(...);
help workspace % for more

>> userinput='toto'

userinput =

toto

>> ws.(userinput) = struct('data',pi)
Value assigned to 'toto'

ws =

  workspace handle

  Properties:
       wstype: 'caller'
      verbose: 1
    localasgn: [1x1 containers.Map]

  Methods, Events, Superclasses

>> toto

toto =

    data: 3.1416

>> ws.('john') = struct('data','A')
Value assigned to 'john'

ws =

  workspace handle

  Properties:
       wstype: 'caller'
      verbose: 1
    localasgn: [2x1 containers.Map]

  Methods, Events, Superclasses

>> whos
  Name Size Bytes Class Attributes

  john 1x1 126 struct
  toto 1x1 132 struct
  userinput 1x4 8 char
  ws 1x1 60 workspace

>> john.data

ans =

A

>> ws.(userinput).data

ans =

    3.1416

% Bruno

Subject: Using a saved string as a variable name?

From: John

Date: 21 Mar, 2010 10:09:03

Message: 5 of 7

Thanks for all the relies,

The reason for doing this is that i need to create a script that will define 6 variables in a .mat file. Say, Cx, Cy, Cz, Cl, Cm, Cn.

Now the process of defining these 6 variables are exactly the same, the difference been with the user's input. Therefore, I want to use a for loop. However, the problem was that each time the for loop loops i need the final result saved to a different variable. (So on the 1st loop it get saved to Cx the 2nd loop Cy and so on.) and i wanted to do this without using a whole series of if ... elseif. Therefore, i was think of saving the variable names into an array and calling the different elements each loop. However, i have yet to get this to work.

 

Subject: Using a saved string as a variable name?

From: David Young

Date: 21 Mar, 2010 10:45:04

Message: 6 of 7

It's not clear why you need each result saved in a different variable. Why not use an array? - it's much simpler. It sounds like a struct array would be right, though a cell array would be another possibility. There is very good documentation for these, so I won't give examples here.

Subject: Using a saved string as a variable name?

From: Bruno Luong

Date: 21 Mar, 2010 12:03:03

Message: 7 of 7

% Put your data as cell

C = {Cx, Cy, Cz, Cl, Cm, Cn}
for Ci=C
  % do domething with Ci{1}
end

% OR in struct
names = {'Cx' 'Cy' 'Cz' 'Cl' 'Cm' 'Cn'}
S = struct('name', names, 'data', {Cx, Cy, Cz, Cl, Cm, Cn})
for Si = S
  % do something with Si.data
end

% Or in containers.Map
names = {'Cx' 'Cy' 'Cz' 'Cl' 'Cm' 'Cn'};
data = {Cx, Cy, Cz, Cl, Cm, Cn};
c = containers.Map(names,data)
for key=names
    ci = c(key{1});
    % do something with ci
end

% Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
structure John 20 Mar, 2010 22:24:04
rssFeed for this Thread

Contact us at files@mathworks.com