How can i assign variable in program.

1 view (last 30 days)
a = [a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20];
assignin('base','a1',x(1));
assignin('base','a2',x(2));
assignin('base','a3',x(3));
assignin('base','a4',x(4));
assignin('base','a5',x(5));
assignin('base','a6',x(6));
assignin('base','a7',x(7));
assignin('base','a8',x(8));
assignin('base','a9',x(9));
assignin('base','a10',x(10));
assignin('base','a11',x(11));
assignin('base','a12',x(12));
assignin('base','a13',x(13));
assignin('base','a14',x(14));
assignin('base','a15',x(15));
assignin('base','a16',x(16));
assignin('base','a17',x(17));
assignin('base','a18',x(18));
assignin('base','a19',x(19));
assignin('base','a20',x(20));
x
  1 Comment
Stephen23
Stephen23 on 15 Dec 2015
Edited: Stephen23 on 3 Nov 2019
In most cases assignin should be avoided for more effiicent ways of passing data between workspaces. The documentation states " Best Practice: Passing Arguments The most secure way to extend the scope of a function variable is to use function input and output arguments, which allow you to pass values of variables."
Numbered variables are a sign that you are doing something wrong.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Dec 2015
You use x but you do not define it.
  2 Comments
Triveni
Triveni on 16 Dec 2015
I want random variable during optimization using fminunc. Can you help me how can i generate random variable during optimization??
Walter Roberson
Walter Roberson on 16 Dec 2015
Using a random variable would result in a discontinuity, which fminunc() is not designed to handle.
Your question is not clear as to whether you want to use a random variable, or a value randomly selected from a list of potential values. If you want to select randomly from the variable named x then
random_x = x(randi(numel(x));

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!