Thread Subject: Assignment of a string to a variable

Subject: Assignment of a string to a variable

From: Andrew

Date: 11 Jan, 2008 15:50:07

Message: 1 of 4

I have used a couple of different methods to achieve the
same goal of assigning a string to a variable. I am
interested to know if there is a better way and which way
that I have presented here is considered better.

Say I have a list of strings that can be configured by a
user to pluck out of the store what data the want:
params={'a';'b'};
I have stored in a file data corresponding to the variables
a, b, etc.
Store.a=[1 2 3];
Store.b=[4 5 6];
Alternatives are 1:
for ind=1:length(params)
    variable=char(strcat(params(ind),'avg'));
assignin('caller',variable,mean(eval(char(strcat('Store.',params(ind))))))
end

or 2:
for ind=1:length(params)
    variable=genvarname(strcat(params(ind),'avg'));
eval([variable
'=mean(eval(char(strcat(''Store.'',params(ind)))))'])
end

I cannot really see the benefit of genvarname here, and I am
not sure of advantages of assignin over eval if any. Is
there a better alternative?

Thanks
Andrew

Subject: Assignment of a string to a variable

From: Titus

Date: 11 Jan, 2008 16:18:33

Message: 2 of 4


"Andrew " <eatmyspam@mathworks.com> schrieb im Newsbeitrag
news:fm837f$edc$1@fred.mathworks.com...
>I have used a couple of different methods to achieve the
> same goal of assigning a string to a variable. I am
> interested to know if there is a better way and which way
> that I have presented here is considered better.
>
> Say I have a list of strings that can be configured by a
> user to pluck out of the store what data the want:
> params={'a';'b'};
> I have stored in a file data corresponding to the variables
> a, b, etc.
> Store.a=[1 2 3];
> Store.b=[4 5 6];
> Alternatives are 1:
> for ind=1:length(params)
> variable=char(strcat(params(ind),'avg'));
> assignin('caller',variable,mean(eval(char(strcat('Store.',params(ind))))))
> end
>
> or 2:
> for ind=1:length(params)
> variable=genvarname(strcat(params(ind),'avg'));
> eval([variable
> '=mean(eval(char(strcat(''Store.'',params(ind)))))'])
> end
>
> I cannot really see the benefit of genvarname here, and I am
> not sure of advantages of assignin over eval if any. Is
> there a better alternative?
>
> Thanks
> Andrew
>

Hi Andrew,
if it is O.K. I would definetely prefer to have the result again
as a cell array or structure:

params = {'a', 'b'};
meanValues = cell(size(params));
for i=1:length(params)
  res{i} = mean(Store.(params{i}));
end

or:
params = {'a', 'b'};
meanValues = [];
for i=1:length(params)
  meanValues.(params{i}) = mean(Store.(params{i}));
end

Both can be abbreviated by using structfun, but I wanted
to show how to use dynamic field names.

Titus


Subject: Assignment of a string to a variable

From: Anh Huy Phan

Date: 11 Jan, 2008 16:24:02

Message: 3 of 4

Using Dynamic Field Names, your code will be more simple

variable = strcat(p,'avg');

for ind=1:length(params)
   assignin('caller',variable{ind},mean(Store.(params{ind})));
end

Anh Huy Phan
RIKEN - BSI









Subject: Assignment of a string to a variable

From: Andrew

Date: 28 Jan, 2008 17:24:20

Message: 4 of 4

"Anh Huy Phan" <phananhhuy@mathworks.com> wrote in message
<fm8572$o0m$1@fred.mathworks.com>...
> Using Dynamic Field Names, your code will be more simple
>
> variable = strcat(p,'avg');
>
> for ind=1:length(params)
> assignin('caller',variable{ind},mean(Store.(params{ind})));
> end
>
> Anh Huy Phan
> RIKEN - BSI
>
Thanks for the replies. The use of dynamic fields helps
because I also want to index selected elements in the data
to be assigned to the variable.

I have a further question and that is weather it is possible
to assign the variable to a structure within assignin such as
assignin('caller',Store.(variable{ind}),mean(Store ...

I know the input should be a string so I tried defining
variable with the structure name already so
variable{1} would be Store.a_avg, but again this did not
work. Failing this I can just store the variable in a
structure after the assignin command.

Thanks for all the help,
Andrew

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
eval Andrew 11 Jan, 2008 10:50:09
variable assignment Andrew 11 Jan, 2008 10:50:09
genvarname Andrew 11 Jan, 2008 10:50:09
assignin Andrew 11 Jan, 2008 10:50:09
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com