0

In Matlab, I want to create a series of variables based on an array of strings.

As a simplified example, from the given matrixes A1, A2, B1, and B2, I want to create variables A and B as their corresponding combined matrixes. Kindly teach me how to do that. It is very much appreciated.

I have tried the below code, which does not work.

% Simplified examples of given matrixes
A1 = [1;2];
A2 = [3;4];
B1 = [5;6];
B2 = [7;8];

% What I need
ATest = cat(1, A1, A2);
BTest = cat(1, B1, B2);

% I have many of A, B, …, thus looking for a loop of string array, but below trying does not work.
VarList = {'A', 'B'};
for ivar = 1:length(VarList)
    eval(['%d = cat(1, %d1, %d2);', VarList(ivar)));
end

Added: (a temporary solution for my desire). Note the use of VarList{ivar} instead of VarList(ivar). I should note it as temporary since caution (for the problem of dynamic variables) may be needed.

VarList = {'A', 'B'};
for ivar = 1:length(VarList)
    v = VarList{ivar};
    eval([v '=cat(1,' v '1,' v '2);']);
end
Romalpa Akzo
  • 599
  • 1
  • 4
  • 12
  • 4
    Those variables should not have been created separately in the first place. They should have been `A = [1 3; 2 4]; B = [5 7; 6 8];`. – beaker Jun 12 '22 at 15:51
  • A1, A2, B1, B2 are just examples. In the actual code, they are complicated matrixes created through a series of calculation. And there are many of them. The main issue that I am struggling at is how to refer to the variable names in combining them. – Romalpa Akzo Jun 12 '22 at 21:00
  • 3
    Of course it is obvious that your example is just a simplified demonstration of your problem. That does not change my statement. The data should never have gotten to the point where this sort of approach would be necessary. I'd much rather concentrate on how your variables are being generated and how to put them in arrays from the start than in teaching you poor programming practices. – beaker Jun 12 '22 at 21:07
  • 1
    If you **must** generate variable names dynamically, there are existing answers on Stack Overflow that will show you how to do that. – beaker Jun 12 '22 at 21:09
  • 3
    I agree with @beaker. You might want to learn about cell arrays as a way to collect arrays into a single, indexable variable. Instead of `A1=…; A2=….` you should write `A{1}=…; A{2}=…`. Then you can easily concatenate them with `cat(1, A{:})`, or access them in turn in a loop. – Cris Luengo Jun 12 '22 at 21:55
  • I need to report out up to 50 matrixes, all of them are created in the same way (combining is just an example) except for their names (A, B, C, D ….., up to 50 names). I am seeking the solution of a loop where I can get that instead of repeating 50 lines. The main issue is that I do not know how to refer to the corresponding variables names. – Romalpa Akzo Jun 12 '22 at 23:16
  • 1
    Here are [some](http://stackoverflow.com/a/32467170/2586922) [nice](https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval) explanations why you should avoid dynamic variable names – Luis Mendo Jun 12 '22 at 23:45
  • 1
    @RomalpaAkzo I understand what you're saying, I just disagree with your approach. If you show us how the matrices are being generated, perhaps we can come up with a better solution for you. Or, as I've said, if you're intent on using 50 different variable names, you can search the answers here on Stack Overflow for a way for you to do that. – beaker Jun 13 '22 at 00:17
  • Many thanks for all of your kind instruction, through which I learn the problem of poor programming practices of dynamic variables. But for now, I've just found a solution to my issue: Utilizing `{}` (instead of `()` to refer to the corresponding string value of the array would give the wanted output to me. – Romalpa Akzo Jun 13 '22 at 00:57
  • I guess my explanation for my desire is unclear. Ultimately, what I need to know is how to refer to each value of a (string) array. To my limited knowledge, (in being cautious of the dynamic variables problem), that reference is still needed sometimes. Thank you very much. – Romalpa Akzo Jun 13 '22 at 01:02
  • I have just added a `temporary` solution in the original post. Kindly advise me of any potential issues for that. – Romalpa Akzo Jun 13 '22 at 01:10
  • 2
    The issues with that solution are clearly explained in the linked Q&As: it’s hard to read and debug, it’s error prone, it deactivates the just-in-time compiler making your whole program slow, … I already explained above what you should do instead. Please don’t be stubborn about this, learn from our experience. – Cris Luengo Jun 13 '22 at 14:41

0 Answers0