MATLAB Answers


Loading variable from whos

Asked by ZK
on 18 Jun 2013

Hi I am working on a group of variables, and I want work on every of them changing them value in loop.

I grouped interested me variables by whos('I2*')

Thank You.



No products are associated with this question.

2 Answers

Answer by Jan Simon
on 18 Jun 2013
 Accepted answer

Avoid such meta-programming. Guessing the names of the variables and construct some obscure EVAL command to access them increases the complexity of your program without any benefit.

If the variables belong together, define them as fields of a struct. Then dynamic field names allow a fast and save processing:

a.x = 1:10;
a.y = rand(10);
fields = fieldnames(a);
for k = 1:length(fields)

  1 Comment

on 19 Jun 2013

Thanks for advise, I used in structure but in different way.

Answer by David Sanchez
on 18 Jun 2013

Try this out:

for k = 1:length(my_vars)

where value_to_assign is jsut that, the value you want the variable to have.

  1 Comment

on 18 Jun 2013

This variables are a matrixes, so I would like to call them directly by name in whos.

Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!