How can I automatically create n variables in a matlab function

30 views (last 30 days)
For the function J_n = Jacobian(n) the user input is n. For each n I want matlab to create a new variable called J_n. For example n=3 returns 3 variables called J_1, J_2 and J_3. Each J_n is a vector containing vectors of Tn' (al T's are already declared in another function don't worry about them). For example n=3 gives J_1 = [T_1' T_0' T_0'] and J_2 = [T_1' T_2' T_0'] and J_3 = [T_1' T_2' T_3']. Note that for n=5 each J_n wil consist of 5 Tn's in stead of 3 Tn's. I tried using for loops, but I was not able to make the name of a variable function dependent.
  1 Comment
Stephen23
Stephen23 on 30 Sep 2015
Edited: Stephen23 on 30 Sep 2015
It is a poor programming practice to generate variable names dynamically and make variables magically pop into existence. Avoid doing this in any programming language.
If you want to know why this is a bad programming practice then read my answer carefully, and all of the linked pages. You might like to research this topic yourself, you will find plenty of discussions online.

Sign in to comment.

Answers (2)

Stephen23
Stephen23 on 30 Sep 2015
Edited: Stephen23 on 19 Jun 2019
  3 Comments
John D'Errico
John D'Errico on 30 Sep 2015
Edited: John D'Errico on 30 Sep 2015
Yes. There is the option to add a comment! Click on the link that says "Comment on this Answer". I've moved your comment to a true comment.
"note: there was no option to comment to the answer of stephen. Thank you for your effort. I accept that dynamic variable names are a bad idea. However I can still not find a solution for my code. Assume I have physical system that can be discribed as M*a=F. M is a matrix. This matrix is a sum of M_0 to M_n. n is the number of degrees of freedom (DoF) for this system, it also is a user input. M_i=J_i*I_i. So I need a different J_i for each M_i and I need to be able to find this J_i. However I dont 'know' what this J_i looks like, because its size and content is dependent on te number of DoF. If generating dynamic variable names is a bad idea, how else am I suppose to 'find' my variable while implementing them in an other variable? Cells and tables etc wont let me do this. Please imagine your old physics exams but now generated automatically by only giving the configuration of the joints and the masses. It really shouldn't be that hard."
Stephen23
Stephen23 on 30 Sep 2015
Edited: Stephen23 on 30 Sep 2015
Store those outputs in a cell array and use indexing to access them:
X = cell(1,N);
X{1} = ...
X{2} = ...

Sign in to comment.


WAT
WAT on 30 Sep 2015
Edited: WAT on 30 Sep 2015
As far as I can tell a cell array will absolutely do what you want it to. For example,
A = cell(1,2);
A(1) = {[1,2]};
A(2) = {[1,2;3,4]};
% etc
Creates a cell array with two different size matrices. To get them out so you can do math with them, just use { }, eg
A2 = eye(2)*A{2}
  4 Comments
Luc Ahrens
Luc Ahrens on 1 Oct 2015
Thank you for you effort. Unfortunately this does not answer my question because with this method I still need to ad content to each A matrix by hand. I want n A matrices automatically generated without adding content manually. The content depends on a consistent pattern that I chose.
WAT
WAT on 1 Oct 2015
Why wouldn't
A{n} = function_that_generates_A(n);
do what you want? Is your issue that you don't know how to generate your A_n matrices, or that you don't know how to store them once you've generated them?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!