From: John on
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
From: ImageAnalyst on
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.

From: Rune Allnor on
On 21 Mar, 03:21, "John " <jsun6...(a)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
From: Bruno Luong on
"John " <jsun6383(a)gmail.com> wrote in message <ho3vqg$4re$1(a)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
From: John on
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.