Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
access variable by string name

Subject: access variable by string name

From: Martin Stolpe

Date: 9 Mar, 2010 16:11:04

Message: 1 of 4

Hello,
I'm exporting some constants from Simulink to the Matlab workspace. The variables are called A1, A2, A3, B1, B2,....

I want to access the variables in this way:

variables_names={'A1', 'A2', 'A3', 'B1'}

for i=1:length(variable_names),
 bi2de(variable_names(1,i))
end;

The variables are arrays of [1,11] size with doubles values. I've tried to use the eval function but didn't get it to work.

Subject: access variable by string name

From: Husam Aldahiyat

Date: 9 Mar, 2010 16:53:04

Message: 2 of 4

"Martin Stolpe" <martin.stople@uni-ulm.de> wrote in message <hn5ruo$rjj$1@fred.mathworks.com>...
> Hello,
> I'm exporting some constants from Simulink to the Matlab workspace. The variables are called A1, A2, A3, B1, B2,....
>
> I want to access the variables in this way:
>
> variables_names={'A1', 'A2', 'A3', 'B1'}
>
> for i=1:length(variable_names),
> bi2de(variable_names(1,i))
> end;
>
> The variables are arrays of [1,11] size with doubles values. I've tried to use the eval function but didn't get it to work.

With cell arrays, you should use {} instead of ().

>> out = eval(['bi2de(',variable_names{1,i},')']);

Subject: access variable by string name

From: Matt J

Date: 9 Mar, 2010 16:58:05

Message: 3 of 4

"Martin Stolpe" <martin.stople@uni-ulm.de> wrote in message <hn5ruo$rjj$1@fred.mathworks.com>...
> Hello,
> I'm exporting some constants from Simulink to the Matlab workspace. The variables are called A1, A2, A3, B1, B2,....
>
> I want to access the variables in this way:
>
> variables_names={'A1', 'A2', 'A3', 'B1'}
>
> for i=1:length(variable_names),
> bi2de(variable_names(1,i))
> end;

I think the following is what you want, but it's hard to tell. Your code doesn't assign the ouput of bi2de to anything

variables_names={'A1', 'A2', 'A3', 'B1'};

for i=1:length(variable_names),

 thisVar=variables_names{i};

 myStruct(thisVar)=bi2de(eval(thisVar));

end;

Subject: access variable by string name

From: Martin Stolpe

Date: 10 Mar, 2010 16:54:04

Message: 4 of 4

Thanks a lot. This solution does what I want. Here is the current code:

% Convert the values from binary coefficients to hex values;

coefficients = {'A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3', 'D13', 'D14', 'D24', 'E21', 'E31', 'E32'};

hex_coeff_array = cell(length(coefficients), 2);

for i=1:length(coefficients),

  thisVar=coefficients{i};
  
  hex_coeff_array(i,1) = cellstr(thisVar);

  hex_coeff_array(i, 2) = cellstr(dec2hex(bi2de(fliplr(eval(thisVar))), 8));
 
end;

Tags for this Thread

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.

Contact us